Asp.net + js 异步上传文件的例子 - 使用iframe实现

[html]  view plain copy print ?
  1. </pre><pre class="html" name="code"><%@ Page Language="C#" AutoEventWireup="true" Codebehind="FileInput.aspx.cs" Inherits="WebApp.FileInput" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>File upload ajax - Demo</title>  
  7.   
  8.     <script type="text/javascript" src="js/jquery.js">js/jquery.js</script>  
  9.   
  10.     <script type="text/javascript">  
  11.     $(function(){  
  12.       
  13.         $("#Button1").click(function(){  
  14.             upload();  
  15.             return false;  
  16.         });  
  17.     });  
  18.       
  19.     function upload()  
  20.     {  
  21.         var of = $("#uploadFile");  
  22.         if(of.val()=="") {  
  23.             alert("Input file for upload!");  
  24.             return;  
  25.         }  
  26.           
  27.         // 添加到表格  
  28.         var tr = $("#fileInfo tr:eq(0)").clone().get(0);  
  29.         tr.cells[0].innerText = $("#fileInfo tr").length;  
  30.         tr.cells[1].innerText = of.val();  
  31.         tr.cells[2].innerHTML = "<a href='javascript:void(0)' οnclick='return remove()'>Delete</a>";   
  32.         $(tr).appendTo("#fileInfo");  
  33.           
  34.         $("#fileContent").empty();  
  35.         $("#fileContent").append(of);  
  36.         $("#filePanel").append(of.clone());  
  37.           
  38.         //修改属性  
  39.         of.attr('id','');  
  40.         of.attr('name','uploadFile');  
  41.   
  42.         $('#uploadForm').submit();  
  43.     }  
  44.       
  45.     function remove()  
  46.     {  
  47.         if(!confirm("Delete now?"))  
  48.         {  
  49.             return false;  
  50.         }  
  51.           
  52.         // 此处做服务器端删除/ajax  
  53.           
  54.         // 界面用JS删除  
  55.         var elm = event.srcElement || event.target;  
  56.         $(elm.parentNode.parentNode).remove();  
  57.           
  58.         return false;  
  59.     }  
  60.     </script>  
  61.   
  62. </head>  
  63. <body>  
  64.     <iframe name="hiddenIframe" frameborder="0" border="0" style="display: none; width: 0px;  
  65.         height: 0px;"></iframe>  
  66.     <form id="uploadForm" action="FileInput.aspx" method="post" enctype="multipart/form-data"  
  67.         target="hiddenIframe" style="display: none; width: 0px; height: 0px;">  
  68.         <span id="fileContent"></span>  
  69.     </form>  
  70.     <form id="form1" runat="server">  
  71.         <div>  
  72.             <asp:Label ID="Label1" runat="server" Text="Input File:"></asp:Label>  
  73.             <span id="filePanel">  
  74.                 <asp:FileUpload ID="uploadFile" runat="server" /></span>  
  75.             <asp:Button ID="Button1" runat="server" Text="Upload File" />  
  76.             <br />  
  77.             <br />  
  78.             <table id="fileInfo" style="border: 1px solid black; width: 400px;">  
  79.                 <tr>  
  80.                     <td>  
  81.                         Row No.  
  82.                     </td>  
  83.                     <td>  
  84.                         File Info……</td>  
  85.                     <td>  
  86.                         Operate</td>  
  87.                 </tr>  
  88.             </table>  
  89.         </div>  
  90.     </form>  
  91. </body>  
  92. </html>  

 

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11.   
  12. namespace WebApp  
  13. {  
  14.     public partial class FileInput : System.Web.UI.Page  
  15.     {  
  16.         protected void Page_Load(object sender, EventArgs e)  
  17.         {  
  18.             if (Request.Files.Count > 0)  
  19.             {  
  20.                 HttpPostedFile file = Request.Files["uploadFile"];  
  21.                 if (null==file)  
  22.                 {  
  23.                     return;  
  24.                 }  
  25.   
  26.                 //Random rand = new Random();  
  27.                 string filename = /*DateTime.Now.Ticks.ToString() + rand.Next() + */System.IO.Path.GetFileName(file.FileName);  
  28.                 string fullFilename = Request.MapPath("~/Files/") + filename;  
  29.   
  30.                 file.SaveAs(fullFilename);  
  31.             }  
  32.         }  
  33.     }  
  34. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值