Asp.Net上传文件
来源:互联网 发布:linux vi 搜索关键字 编辑:程序博客网 时间:2024/06/05 17:48
在使用Asp.Net上传文件时 ,注意要将form中的enctype属性设置为multipart/form-data,然后才能将选中的文件以流的方式上传到服务器端, enctype默认属性是application/x-www-form-urlencoded,当已默认属性进行上传时上传的只是文件名,并没有将数据上传到服务器。
注意:Asp.Net中允许上传的文件最大为4m,超过4m文件上传,请求会自动取消。
如:
<form method="post" action="ShowFile.ashx" enctype="multipart/form-data"> 请选择要上传的文件:<input type="file" name="file" /> <input type="submit" value="上传文件" /> </form>`
在对用户上传的图片或其他文件可以按照上传日期进行保存,这样方便管理,还可以将文件路径保存到数据库中,便于日后调用。
如
// 接收到传过来的数据 HttpPostedFile imageFile = context.Request.Files["file"]; if (imageFile==null) { context.Response.Write("请选择要上传的文件"); } else { //获取上传文件的文件名和拓展名 string filePath = Path.GetFileName(imageFile.FileName); string fileExt= Path.GetExtension(filePath); //判断拓展名 if (fileExt==".jpg"||fileExt==".png") { //将图片按照上传的日期进行保存到相对应的路径 string newFilePath = "/上传文件/Image/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; string newFileName = Guid.NewGuid().ToString(); //判断要保存的文件夹是否已创建Directory.CreateDirectory(Path.GetDirectoryName(context.Request.MapPath(newFilePath))); //构建新的文件夹路径和文件名 string fullDir = newFilePath + newFileName + fileExt; //进行保存imageFile.SaveAs(context.Request.MapPath(fullDir)); } else { context.Response.Write("请上传正确的文件格式"); }
阅读全文
0 0
- asp.net文件上传
- ASP.Net文件上传
- ASP.Net文件上传
- ASP.NET上传文件
- asp.net文件上传
- ASP.NET上传文件
- Asp.Net 文件上传
- asp.net上传文件
- ASP.NET上传文件
- asp.net上传文件
- asp.net文件上传
- ASP.NET上传文件
- ASP.NET文件上传
- ASP.NET上传文件
- asp.net上传文件
- 文件上传(asp.net)
- ASP.NET文件上传
- ASP.NET 文件上传
- 【python+autohotkey】优雅地在markdown中插入图片
- Linux安装mysql
- TCP/IP的原理与介绍
- [SDUT](2088)refresh的停车场 ---栈和队列
- MySQL学习笔记
- Asp.Net上传文件
- spring cloud config非对称加密不能自动解密
- php强化练习(1)——数组
- 【NOIP模板】 线段树
- 学习安装unik环境——过程一
- hadoop安装完后打开网页http://localhost:50070/ 没反应
- (2)学习SrpingBoot 之 返回Json数据
- 解决嵌套滑动的卡顿,Android嵌套滑动机制(NestedScrolling)
- Python3中list排序