實現ASP.NET中FileUpload多檔案上傳

iDotNetSpace發表於2009-10-19

這裡附加一下上傳單個檔案的CS程式碼:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtprotected void upload_Click(object sender, EventArgs e)
    {

        
if (upfile.HasFile)
        {
            
string fname = upfile.PostedFile.FileName;
            
int fi = fname.LastIndexOf("\\"+ 1;
            
string filename = fname.Substring(fi);
            upfile.PostedFile.SaveAs(Server.MapPath(
"upload\\" + filename));
            Response.Write(
"");
        }
        
else
        {
            Response.Write(
"");
        }

    }

下面是上傳多個檔案的全部程式碼,第一次實現這樣的功能,難免有考慮不周全的地方,還望高手見到後指教!

【顯示頁面程式碼】:

標籤中插入如下指令碼程式碼:

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt<script type="text/javascript">
         
function addfile()
         {
          
var uploadfiles=document.getElementById("uploadfiles"),
    str 
= '';
          uploadfiles.innerHTML
+=str;
         }
</script>

 

在頁面主體部分插入:

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt<div>
     
<id="uploadfiles"><input type="file" size="50" name="file">p>
     
<input onclick="addfile()" type="button" value="增加">
     
<asp:button id="uploadbutton" Text="開始上傳" Runat="server">asp:button>
div>

 

【C#程式碼】:

新增using指令:using System.Text.RegularExpressions;

在protected void Page_Load(object sender, EventArgs e){}中插入如下程式碼:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtHttpFileCollection files = HttpContext.Current.Request.Files;
        
//狀態資訊
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        
for (int ifile = 0; ifile < files.Count; ifile++)
        {
            HttpPostedFile postedfile 
= files[ifile];
            
string filename, fileExt;
            filename 
= System.IO.Path.GetFileName(postedfile.FileName);    //獲取檔名
            fileExt = System.IO.Path.GetExtension(filename);    //獲取檔案字尾

            
int MaxAllowUploadFileSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.
AppSettings[
"MaxAllowUploadFileSize"]);     //定義允許上傳檔案大小
            if (MaxAllowUploadFileSize == 0) { MaxAllowUploadFileSize = 26500; }
            
string allowexts = System.Configuration.ConfigurationSettings.AppSettings["AllowUploadFileType"]; 
 
//定義允許上傳檔案型別
            if (allowexts == "") { allowexts = "doc|docx|rar|xls|xlsx|txt"; }
            Regex allowext 
= new Regex(allowexts);

            
if (postedfile.ContentLength < MaxAllowUploadFileSize && allowext.IsMatch(fileExt)) //檢查檔案大小及副檔名
            {
                postedfile.SaveAs(Server.MapPath(
"upload\\" + filename + fileExt));    //upload為與本頁面同一目錄,可自行修改
            }
            
else
            {
                Response.Write(
"
alert('不允許上傳型別</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;fileExt&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">或檔案過大')
");
            }
        }

【Web.config中程式碼】:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt<appSettings>
    
<add key="MaxAllowUploadFileSize" value="256000" />
    
<add key="AllowUploadFileType" value="doc|docx|rar|xls|xlsx|txt" />
appSettings>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-616848/,如需轉載,請註明出處,否則將追究法律責任。

相關文章