asp.net图片上传代码


 

前端:

<form action="/ImageUpload.ashx" method="post" enctype="multipart/form-data">

<input type="file" name="imgFile" />
<input type="submit" value="上传文件" />

</form>

后台:

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
HttpPostedFile file = context.Request.Files["imgFile"];

string extName = System.IO.Path.GetExtension(file.FileName);
if(extName!=".jpeg" && extName!=".gif" && extName!=".jpg" && extName!=".png" )
{
context.Response.Write("文件格式不合法!");
return;
}

string uploadImgName = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
file.SaveAs(context.Request.MapPath(uploadImgName));

context.Response.Write("上传成功!");

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM