最简单的fileupload控件上传图片例子
代码如下 | 复制代码 |
using System.IO; |
上面的没做文件大小判断只做了图片类型了,如果要判断大小我们需要如下设置
在web.config中配置:
代码如下 | 复制代码 |
|
在.cs文件中方法实现:
文件大小判断:
代码如下 | 复制代码 |
public bool IsAllowableFileSize() |
好了,看了上面我们看一个完整的例子
代码如下 | 复制代码 |
using System; private string PICTURE_FILE = "[.gif.png.jpeg.jpg]"; private int IMG_MAX_WIDTH = 700;//指定宽度 /// /// /// /// /// public bool UploadImage(int maxWidth, int maxHeight) /// /// /// private bool DoUpload(string allowedExtensions) if (!_fileUpload.HasFile) throw new Exception("没有文件!"); //上传控件中如果不包含文件,退出 // 得到文件的后缀 // 看包含的文件是否是被允许的文件后缀 //检查上传文件大小 try // 检查图片宽度/高度/大小 Bitmap bmp = new Bitmap(output); this.CreateDir(Path.GetDirectoryName(savefile)); bmp.Save(savefile, output.RawFormat); if (_AutoGenWatermark) _fileUpload.PostedFile.SaveAs(savefile); private void CreateDir(string dir) private bool IsUploadImage(string fileExtension) |
上面可以说己完整的介绍了利用 fileupload控件实现的单个文件上传了,如果要多文件上传呢怎么办?我们接着往下看。
同时上载多个文件cs处理代码
代码如下 | 复制代码 |
protected void Button1_Click(object sender, EventArgs e) { string filepath = "C:/Uploads"; //HttpFileCollection类型 HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { //HttpPostedFile类型 HttpPostedFile userPostedFile = uploadedFiles[i]; try { if (userPostedFile.ContentLength > 0 ) { Label1.Text += "File #" + (i+1) + ""; Label1.Text += "File Content Type: " + userPostedFile.ContentType + ""; Label1.Text += "File Size: " + userPostedFile.ContentLength + "kb"; Label1.Text += "File Name: " + userPostedFile.FileName + ""; userPostedFile.SaveAs(filepath + "/" + System.IO.Path.GetFileName(userPostedFile.FileName)); Label1.Text += "Location where saved: " + filepath + "/" + System.IO.Path.GetFileName(userPostedFile.FileName) + ""; } } catch (Exception Ex) { Label1.Text += "Error: " + Ex.Message; } } } |
同时多文件上传核心代码是int i = 0; i < uploadedFiles.Count; i++遍历了,其它的与单文件上传没有区别。
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级