方便了。和大家分享下。
所需控件:
FileUpload上传文件Button 实现上传Label 显示上传文件信息主要是上传按钮的事件:protected void Button1_Click(object sender, EventArgs e) { string fullname = FileUpload1.FileName.ToString();//直接取得文件名 string url = FileUpload1.PostedFile.FileName.ToString();//
取得上传文件路径 string typ = FileUpload1.PostedFile.ContentType.ToString
();//获取文件MIME内容类型 string typ2 = fullname.Substring(fullname.LastIndexOf(".")
+ 1);//获取文件名字 . 后面的字符作为文件类型 int size = FileUpload1.PostedFile.ContentLength;
//下面是保存了,我们来个判断,只能上穿传哪些格式的文件吧 if (File.Exists(url)) { Response.Write("<script>alert('文件已存在 !')
</script>"); } else { if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" ||
typ2 == "png" || typ2 == "rar") //if
(typ=="image/bmp"||typ=="image/gif"||typ=="image/jpeg") { if (size <= 4134904) { FileUpload1.SaveAs(Server.MapPath("upload") +
"\\" + fullname);//Server.MapPath("uploads\\image") + "\\" +
fullname将文件保存在跟目录的uploads文件夹下 Label1.Text = ("你上传了一个名为" + url + ",
MIME内容类型为[" + typ + "],后缀名为[" + typ2 + "],共[size]字节
大小的文件<br>已经保存在了你的UP目录中了,如果你是用VS2005,请刷新
你的目录哦!!"); } else { Response.Write("<script>alert('你的文件超过限
制大小!')</script>"); }
} else { Label1.Text = "上传文件格式不正确.."; }
} }
个人建议用ContentType判断上传文件类型,相对安全,防止别人上传恶意文件。
.net中默只能上传小于4m的文件,大于4M将无法显示页面,显示为空白页。关于文件大小的设置,主要是通过配置Web.config:
在web.config中添加httpRuntime元素,代码如下:<configuration> <system.web> <httpRuntime maxRequestLength="8192" useFullyQualifiedRedirectUrl="true" executionTimeout="45" versionHeader="1.1.4128"/> </system.web></configuration>其中maxRequestLength属性就是限制上传大小的,如设为"8192"即为8M。 ContentType MIME内容类型的全部类型列表 application/cdfapplication/fractalsapplication/futuresplashapplication/htaapplication/mac-binhex40application/msaccessapplication/mswordapplication/pdfapplication/pkcs10application/pkcs7-mimeapplication/pkcs7-signatureapplication/pkix-certapplication/pkix-crlapplication/postscriptapplication/set-payment-initiationapplication/set-registration-initiationapplication/vnd.adobe.xfdfapplication/vnd.fdfapplication/vnd.ms-excelapplication/vnd.ms-pki.certstoreapplication/vnd.ms-pki.pkoapplication/vnd.ms-pki.seccatapplication/vnd.ms-pki.stlapplication/vnd.ms-powerpointapplication/vnd.rmfapplication/vnd.visioapplication/x-cdfapplication/x-complusapplication/x-compressapplication/x-compressedapplication/x-gzipapplication/x-internet-signupapplication/x-iphoneapplication/x-latexapplication/x-mix-transferapplication/x-mplayer2application/x-ms-wmdapplication/x-ms-wmsapplication/x-ms-wmzapplication/x-msexcelapplication/x-mspowerpointapplication/x-pkcs12application/x-pkcs7-certificatesapplication/x-pkcs7-certreqrespapplication/x-shockwave-flashapplication/x-stuffitapplication/x-tarapplication/x-troff-manapplication/x-x509-ca-certapplication/x-zip-compressedapplication/xmlaudio/aiffaudio/basicaudio/midaudio/midiaudio/mp3audio/mpegaudio/mpegurlaudio/mpgaudio/wavaudio/x-aiffaudio/x-backgroundaudio/x-midaudio/x-midiaudio/x-mp3audio/x-mpegaudio/x-mpegurlaudio/x-mpgaudio/x-ms-waxaudio/x-ms-wmaaudio/x-wavimage/bmpimage/gifimage/jpegimage/pjpegimage/pngimage/svgimage/svg+xmlimage/svg-xmlimage/tiffimage/tiff2image/x-iconimage/x-jgimage/x-pngimage/x-wmfimage/x-xbitmapimage/xbmimage/xiffimage/xiff2message/rfc822midi/midtext/csstext/h323text/htmltext/iulstext/plaintext/scriptlettext/webviewhtmltext/x-componenttext/x-scriptlettext/x-vcardtext/xmlvideo/avivideo/mpegvideo/mpgvideo/msvideovideo/quicktimevideo/x-ivfvideo/x-mpegvideo/x-mpeg2avideo/x-ms-asfvideo/x-ms-asf-pluginvideo/x-ms-wmvideo/x-ms-wmpvideo/x-ms-wmvvideo/x-ms-wmxvideo/x-ms-wvxvideo/x-msvideo