上传多个文件aspx.net C#

时间:2013-03-29 10:50:45

标签: c# asp.net file-upload image-uploading multifile-uploader

我有以下HTML源代码:

 <form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server">


       <input type="file" name="file1"/><br />
            <input type="file" style="margin-right: 52px;" name="file2" /><br />
            <input type="file" style="margin-right: 52px;" name="file3" /><br />
            <input type="file" style="margin-right: 52px;" name="file4" /><br />
        <button type="submit" class="blue-button">הוסף מסלול</button>
    </form>

使用此ASPX - C#代码:

if (Request.ContentLength != 0)
{
    int Size = Request.Files[0].ContentLength / 1024;
    if (Size <= 512)
    {

        string LocalFile = Request.Files[0].FileName;
        int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
        string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
        string Path = Server.MapPath(" ../images/tracks") + "..\\" + File;
        Request.Files[0].SaveAs(Path);
        Response.Write(@"The file was saved: " + Path);
    }
    else
    {
        Response.Write("The file is too big !");
    }
}
else
{
    Response.Write("Unknown Error !");
}

如果我上传了一个文件,效果很好,但我上传的是多个上传输入无法正常工作。

我的问题是为什么以及如何解决?

2 个答案:

答案 0 :(得分:2)

据我所见,您只需要在表单中添加enctype="multipart/form-data"

 <form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server" enctype="multipart/form-data">

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

  

内容类型“application / x-www-form-urlencoded”效率低下   用于发送大量二进制数据或包含的文本   非ASCII字符。 内容类型“multipart / form-data”应为   用于提交包含文件,非ASCII数据和表单的表单   二进制数据。

您没有使用自动添加该类型的asp:FileUpload控件,因此您应该手动执行此操作。

for(int i = 0; i < Request.Files.Count; i++) {

    int Size = Request.Files[i].ContentLength / 1024;
    if (Size <= 512)
    {
       string LocalFile = Request.Files[i].FileName;
    //.....
}

答案 1 :(得分:0)

我建议您使用此uploadify library,它是免费的

因为基本的上传文件asp.net不提供多次下载的可能性

link:http://www.uploadify.com/download/

相关问题