c# 上传图片
javascript 现在图片之后先显示出来(不支持firefox)
function change_photo(obj,width,height)
{
var imgDIV = document.getElementById("imgDIV");
var str = obj.value || obj;
try{
imgDIV.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = str;
imgDIV.style.width=width;
imgDIV.style.height=height;
imgDIV.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").sizingMethod = 'scale';
document.getElementById('img').style.display = "none";
}
catch(e)
{
// 上传不是图片文件的处理。
}
}
<div id="imgDIV" style="width:258px;height:63px;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);
text-align: center;">
<img ID="img"><!--height="300px" Width="500px" nerror="change_photo(this.src,140,70);"-->
</div>
<input type="file" name="files"
onpropertychange="change_photo(this,800,600);" />
=====================================
Boolean fileOK = false;
String path = Server.MapPath("/NewFolder/");
string strFileType="";
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
strFileType=fileExtension;
break;
}
}
}
if (fileOK)
{
try
{
//FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
string strFileName = System.DateTime.Now.ToString("yyyyMMdd")+(new Random()).Next(100) + strFileType;
FileUpload1.PostedFile.SaveAs(path + strFileName);
//Label1.Text = "File uploaded!";
Image1.ImageUrl = "/NewFolder/" + strFileName;
//Page.RegisterStartupScript("", "<script>alert('上传成功!')</script>");
}
catch (Exception ex)
{
//Label1.Text = "File could not be uploaded.";
Page.RegisterStartupScript("", "<script>alert('上传失败!')</script>");
}
}
else
{
//Label1.Text = "Cannot accept files of this type.";
Page.RegisterStartupScript("", "<script>alert('上传文件类型错误!')</script>");
}