欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

asp.net type=file前后台合作 在上传图片到服务器

发布时间:2025/4/14 asp.net 63 豆豆
生活随笔 收集整理的这篇文章主要介绍了 asp.net type=file前后台合作 在上传图片到服务器 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前台

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title> </head> <body><form runat="server" id="form1" method="post" enctype="multipart/form-data"><input name="f" type="file" /><input name="s" type="submit" /></form> </body> </html>

 

 

后台

System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;if (_file.Count > 0){//文件大小long size = _file[0].ContentLength;//文件类型string type = _file[0].ContentType;//文件名string name = _file[0].FileName;//文件格式string _tp = System.IO.Path.GetExtension(name);if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf"){//获取文件流System.IO.Stream stream = _file[0].InputStream;//保存文件string saveName = DateTime.Now.ToString("yyyyMMddHHmmss") + _tp;string path = DataFactory.WFile.FileUploadPath + "/upload/area/" + saveName; // string path = Server.MapPath("images/"+savename);自己编写_file[0].SaveAs(path);}}

总结:

1.form 必须有runat="server"标记,

2.form  必须有enctype="multipart/form-data"标记,

3.<input type="file" />的runat="server"标记可选

 

完!

 

转载于:https://www.cnblogs.com/wwz-wwz/p/6244725.html

总结

以上是生活随笔为你收集整理的asp.net type=file前后台合作 在上传图片到服务器的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。