多个文件上传. [英] Multiple File Upload.

查看:75
本文介绍了多个文件上传.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从
获得了以下多文件上传代码 http://www.asp.net/general/videos/how-do-i-multiple-file-uploads-in-aspnet-2 [

I got the following code of Multiple file upload from
http://www.asp.net/general/videos/how-do-i-multiple-file-uploads-in-aspnet-2[^].

But there is an error in the C# (ie. HttpPostedFile has not assigned properly).
Can anyone correct this. This will be very helpful for me.
//============================================================================

<form id="form1" runat="server" enctype="multipart/form-data">
<p id="upload-area">
   <input id="File1" type="file" runat="server" size="60" />
</p>

<input id="AddFile" type="button" value="Add file" onclick="addFileUploadBox()" />
<p><asp:Button ID="btnSubmit" runat="server" Text="Upload Now" OnClick="btnSubmit_Click" /></p>
<span id="Span1" runat="server" />

<script type="text/javascript">
function addFileUploadBox()
{
    if (!document.getElementById || !document.createElement)
        return false;

    var uploadArea = document.getElementById ("upload-area");

    if (!uploadArea)
        return;

    var newLine = document.createElement ("br");
    uploadArea.appendChild (newLine);

    var newUploadBox = document.createElement ("input");

    // Set up the new input for file uploads
    newUploadBox.type = "file";
    newUploadBox.size = "60";

    // The new box needs a name and an ID
    if (!addFileUploadBox.lastAssignedId)
        addFileUploadBox.lastAssignedId = 100;

    newUploadBox.setAttribute ("id", "dynamic" + addFileUploadBox.lastAssignedId);
    newUploadBox.setAttribute ("name", "dynamic:" + addFileUploadBox.lastAssignedId);
    uploadArea.appendChild (newUploadBox);
    addFileUploadBox.lastAssignedId++;
}
</script>
</form>


//================================================ ========================


//==========================================================================

using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String UpPath;
        UpPath = "C:\\UploadedUserFiles";
        
        if (!Directory.Exists(UpPath))
        {
            Directory.CreateDirectory("C:\\UploadedUserFiles\\");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpFileCollection uploads = HttpContext.Current.Request.Files;
        for (int i = 0; i < uploads.Count; i++)
        {
            HttpPostedFile upload = file;// Here is the Error.....

            if (upload.ContentLength == 0)
                continue;

            string c = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.

        try
            {
            upload.SaveAs("C:\\UploadedUserFiles\\" + c);
            Span1.InnerHtml = "Upload(s) Successful.";
            }
        catch(Exception Exp)
            {
                Span1.InnerHtml = "Upload(s) FAILED.";
            }
        }
    }
}

推荐答案

但是file是什么?您尚未分配或什至没有声明它.请改用以下内容.
But what is file? You haven''t assigned or not even declared it. Use following instead.
HttpPostedFile upload = uploads[i];


这篇关于多个文件上传.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆