Blueimp jQuery文件上传与Asp.Net集成

时间:2013-05-07 10:08:32

标签: jquery asp.net

我正在尝试使用Asp.Net上传jquery blueimp多文件。 经过两天不成功的尝试,我决定问可能有人会分享工作实例。

我将非常感激。

1 个答案:

答案 0 :(得分:4)

您可以使用确切的演示文件,而无需使用先前评论中链接的.NET示例。将表单操作链接更改为指向您创建的handler.ashx文件。

因此,在index.html的表单中,将url添加到您创建的处理程序作为操作:

<form id="fileupload" action="GongosHandler.ashx" method="POST" enctype="multipart/form-data">

然后执行一个基本处理程序,编码如下:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";//"application/json";
    var r = new System.Collections.Generic.List<ViewDataUploadFilesResult>();


    for (var x = 0; x < context.Request.Files.Count; x++)
    {
        HttpPostedFile hpf = context.Request.Files[x] as HttpPostedFile;
        string FileName = string.Empty;
        if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
        {
            string[] files = hpf.FileName.Split(new char[] { '\\' });
            FileName = files[files.Length - 1];
        }
        else
        {
            FileName = hpf.FileName;
        }
        if (hpf.ContentLength == 0)
            continue;
        string savedFileName = context.Server.MapPath("~/Uploads/" + FileName);
        try
        {
            hpf.SaveAs(savedFileName);
        }
        catch (Exception ex)
        {

        }


        r.Add(new ViewDataUploadFilesResult()
        {
            thumbnailUrl = savedFileName,
            name = FileName,
            length = hpf.ContentLength,
            type = hpf.ContentType,
            url =  string.Format("/Uploads/{0}", FileName),
            deleteUrl =  string.Format("/Uploads/{0}", FileName) 
        });
        var uploadedFiles = new
        {
            files = r.ToArray()
        };

        //was returning a new json string everytime, so then duplicating if 
        //sending multiple files. Example, file 1 was in first position,
        //then file 1 & 2 in second position, and so on. So, I only grab,
        //the last JSON instance to get all files.
        if (x  == (context.Request.Files.Count - 1))
        {
            var jsonObj = JsonConvert.SerializeObject(uploadedFiles, Formatting.Indented);
            string jsonHttpOutputStream = jsonObj.ToString();

            context.Response.Write(jsonHttpOutputStream);
        }
    }

}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

public class ViewDataUploadFilesResult
{
    public string thumbnailUrl { get; set; }
    public string name { get; set; }
    public int length { get; set; }
    public string type { get; set; }
    public string url { get; set; }
    public string deleteUrl { get; set; }
}

在基本层面上,这将呈现并发挥作用。然后,你必须在这里和那里调整一些代码,以使它按照你想要的方式运行。但是,这个处理程序是您正确处理上传所需的全部内容。祝你好运!