實現ASP.NET中FileUpload多檔案上傳
這裡附加一下上傳單個檔案的CS程式碼:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->protected 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/
-->protected 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/
--><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/
--><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/
--><div>
<p 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>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><div>
<p 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/
-->HttpFileCollection 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("
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->HttpFileCollection 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"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> fileExt </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">或檔案過大')
");}
}
【Web.config中程式碼】:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><appSettings>
<add key="MaxAllowUploadFileSize" value="256000" />
<add key="AllowUploadFileType" value="doc|docx|rar|xls|xlsx|txt" />
appSettings>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在ASP.NET中實現多檔案上傳 (轉)ASP.NET
- HttpFileCollection 實現多檔案上傳HTTP
- SpringMVC 通過commons-fileupload實現檔案上傳SpringMVC
- PHP實現單檔案、多檔案上傳 封裝 物件導向實現檔案上傳PHP封裝物件
- 7.1、使用Commons Fileupload上傳檔案
- struts動態多檔案上傳實現
- SpringMVC多個檔案上傳實現SpringMVC
- SpringMVC實現多檔案上傳原始碼SpringMVC原始碼
- node中間層實現檔案上傳
- ASP.NET 2.0 多檔案上傳小經驗ASP.NET
- FileUpload上傳多檔案時出現“無法訪問已關閉的檔案”錯誤的解決方法
- ajax實現檔案上傳
- FileUpload 上傳的檔案獲取相對路徑
- Android 用Retrofit 2實現多檔案上傳實戰Android
- spring-boot-route(三)實現多檔案上傳Springboot
- SpringMVC 單檔案上傳與多檔案上傳SpringMVC
- ASP.NET Core 檔案上傳ASP.NET
- AngularJS實現的檔案檔案上傳AngularJS
- 檔案上傳原理和實現
- springmvc實現檔案上傳SpringMVC
- Struts2框架的檔案上傳--common-fileupload框架
- laravel 多檔案上傳Laravel
- ASP.NET中檔案上傳下載方法集合ASP.NET
- Vue實現多檔案上傳功能(前端 + 後端程式碼)Vue前端後端
- Jsp+Servlet實現檔案上傳下載(一)--檔案上傳JSServlet
- 通過配置檔案(.htaccess)實現檔案上傳
- 使用java的MultipartFile實現layui官網檔案上傳實現全部示例,java檔案上傳JavaUI
- js實現帶上傳進度的檔案上傳JS
- 用ASP.NET上傳大檔案ASP.NET
- MVC檔案上傳 - 使用Request.Files上傳多個檔案MVC
- PHP實現圖片(檔案)上傳PHP
- Java檔案上傳如何實現呢?Java
- 關於node實現檔案上傳
- 使用Spring實現上傳檔案Spring
- Spring mvc檔案上傳實現SpringMVC
- JS實現檔案自動上傳JS
- SPS中JSOM和SOAP 實現檔案上傳JS
- 用Go語言實現多協程檔案上傳,斷點續傳,你如何實現?Go斷點