文件上传
1. 普通文件上传
View Code
1 <form id="form1" action="UploadTest1.aspx" method="post" enctype="multipart/form-data"> 2 <input name="Action" value="From" type="hidden"/> 3 <input type="file" name="file" /> <br /> 4 <input type="submit" value="提交" /> 5 </form>
View Code
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 string action = Request.Form.Get("Action"); 4 if (action == "From") 5 { 6 string test = Request["aaa"]; 7 HttpPostedFile file = Request.Files.Get(0); 8 //Response.Write(file.FileName); 9 //Response.Write(file.ContentLength); 10 byte[] buffer = new byte[1024]; 11 string content = string.Empty; 12 file.SaveAs(Server.MapPath("~/data/") + file.FileName); 13 //using (Stream stream = file.InputStream) 14 //{ 15 // using (MemoryStream memory = new MemoryStream()) 16 // { 17 // int n = file.InputStream.Read(buffer, 0, 1024); 18 // while (n > 0) 19 // { 20 // memory.Write(buffer, 0, n); 21 // n = file.InputStream.Read(buffer, 0, 1024); 22 // } 23 // content = Encoding.Default.GetString(memory.ToArray()); 24 // } 25 //} 26 //Response.Write(content); 27 28 } 29 }
2 asp.net 服务器控件上传
View Code
1 <form id="form1" runat="server"> 2 <div> 3 <asp:FileUpload ID="FileUpLoad1" runat="server" /> 4 <br /> 5 <asp:Button ID="btnFileUpload" runat="server" OnClick="btnFileUpload_Click" Text="文件上传" 6 OnClientClick="return checkType()" /> 7 <asp:Label ID="lblMessage" runat="server"></asp:Label> 8 </div> 9 </form>
View Code
1 //文件上传按钮click事件 2 protected void btnFileUpload_Click(object sender, EventArgs e) 3 { 4 if (FileUpLoad1.HasFile) 5 { 6 //判断文件是否小于10Mb 7 if (FileUpLoad1.PostedFile.ContentLength < 10485760) 8 { 9 try 10 { 11 //上传文件并指定上传目录的路径 12 FileUpLoad1.PostedFile.SaveAs(Server.MapPath("~/data/") 13 + FileUpLoad1.FileName); 14 /*注意->这里为什么不是:FileUpLoad1.PostedFile.FileName 15 * 而是:FileUpLoad1.FileName? 16 * 前者是获得客户端完整限定(客户端完整路径)名称 17 * 后者FileUpLoad1.FileName只获得文件名. 18 */ 19 20 //当然上传语句也可以这样写(貌似废话): 21 //FileUpLoad1.SaveAs(@"D:\"+FileUpLoad1.FileName); 22 23 lblMessage.Text = "上传成功!"; 24 } 25 catch (Exception ex) 26 { 27 lblMessage.Text = "出现异常,无法上传!"; 28 //lblMessage.Text += ex.Message; 29 } 30 31 } 32 else 33 { 34 lblMessage.Text = "上传文件不能大于10MB!"; 35 } 36 } 37 else 38 { 39 lblMessage.Text = "尚未选择文件!"; 40 } 41 }
3 利用iframe 无刷新上传
View Code
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head runat="server"> 3 <script src="Script/jquery-1.4.1.js" type="text/javascript"></script> 4 <title></title> 5 </head> 6 <body> 7 <div id="msg"></div> 8 <form id="form1" action="Handler/Upload3Handler.ashx" method="post" enctype="multipart/form-data" target="hidden_frame"> 9 <input name="Action" value="From" type="hidden" /> 10 <input type="file" name="file" /> 11 <br /> 12 <input type="submit" value="提交" /> 13 </form> 14 <iframe name="hidden_frame" id="hidden_frame" style="display: none"></iframe> 15 <a href="UploadTest1.aspx" target="view_window">123</a> 16 </body> 17 </html> 18 <script type="text/javascript"> 19 function callback(msg) { 20 document.getElementById("msg").innerHTML = "<font color=red>" + msg + "</font>"; 21 } 22 </script>
View Code
1 HttpPostedFile file = context.Request.Files.Get(0); 2 //Response.Write(file.FileName); 3 //Response.Write(file.ContentLength); 4 byte[] buffer = new byte[1024]; 5 string content = string.Empty; 6 file.SaveAs(context.Server.MapPath("~/data/") + file.FileName); 7 //using (Stream stream = file.InputStream) 8 //{ 9 // using (MemoryStream memory = new MemoryStream()) 10 // { 11 // int n = file.InputStream.Read(buffer, 0, 1024); 12 // while (n > 0) 13 // { 14 // memory.Write(buffer, 0, n); 15 // n = file.InputStream.Read(buffer, 0, 1024); 16 // } 17 // content = Encoding.Default.GetString(memory.ToArray()); 18 // } 19 //} 20 //Response.Write(content); 21 context.Response.Write(string.Format("<script>parent.callback('fileName: {0} size :{1}')</script>", file.FileName, file.ContentLength)); 22 context.Response.End();
4 简单的swfUpload 上传
View Code
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head runat="server"> 3 <link href="Script/Util/swfupload/js/default.css" rel="stylesheet" type="text/css" /> 4 <script src="Script/Util/swfupload/js/swfupload.js" type="text/javascript"></script> 5 <script src="Script/Util/swfupload/js/swfupload.queue.js" type="text/javascript"></script> 6 <script src="Script/Util/swfupload/js/fileprogress.js" type="text/javascript"></script> 7 <script src="Script/Util/swfupload/js/handlers.js" type="text/javascript"></script> 8 <title></title> 9 </head> 10 <body> 11 <div id="content"> 12 <form id="form1" action="UploadTest5.aspx" method="post" enctype="multipart/form-data"> 13 <div class="fieldset flash" id="fsUploadProgress"> 14 </div> 15 <div id="divStatus"> 16 </div> 17 <div> 18 <span id="spanButtonPlaceHolder"></span> 19 <input id="btnCancel" type="button" value="取消上传" onclick="swfu.cancelQueue();" disabled="disabled" 20 style="margin-left: 2px; font-size: 8pt; height: 29px;" /> 21 </div> 22 <input type="submit" value="提交" /> 23 </form> 24 </div> 25 </body> 26 </html> 27 <script type="text/javascript"> 28 var swfu; 29 30 window.onload = function () { 31 var settings = { 32 flash_url: "Script/Util/swfupload/js/swfupload.swf", 33 upload_url: "Handler/Upload3Handler.ashx", 34 post_params: { "param": "参数" }, 35 file_size_limit: "800 MB", 36 file_types: "*.*", 37 file_types_description: "All Files", 38 file_upload_limit: 100, 39 file_queue_limit: 0, 40 custom_settings: { 41 progressTarget: "fsUploadProgress", 42 cancelButtonId: "btnCancel" 43 }, 44 debug: false, 45 46 // Button settings 47 button_image_url: "Script/Util/swfupload/images/TestImageNoText_65x29.png", 48 button_width: "65", 49 button_height: "29", 50 button_placeholder_id: "spanButtonPlaceHolder", 51 button_text: '<span class="theFont">Hello</span>', 52 button_text_style: ".theFont { font-size: 16; }", 53 button_text_left_padding: 12, 54 button_text_top_padding: 3, 55 56 // The event handler functions are defined in handlers.js 57 file_queued_handler: fileQueued, 58 file_queue_error_handler: fileQueueError, 59 file_dialog_complete_handler: fileDialogComplete, 60 upload_start_handler: uploadStart, 61 upload_progress_handler: uploadProgress, 62 upload_error_handler: uploadError, 63 upload_success_handler: uploadSuccess, 64 upload_complete_handler: uploadComplete 65 // queue_complete_handler: queueComplete // Queue plugin event 66 }; 67 68 swfu = new SWFUpload(settings); 69 }; 70 71 72 </script>
5 swfUpload 同步显示图片的上传
https://code.google.com/p/swfupload/downloads/list
备注:在web.config 设置文件上传限制
<httpRuntime maxRequestLength="2097151" executionTimeout="3600"/>
swfUpload dome
http://demo.swfupload.org/v220/
swfUpload 文档
http://www.leeon.me/upload/other/swfupload.html