使用Fine Uploader以块的形式上传文件,我无法将它们合并在一起

时间:2013-04-02 22:40:00

标签: c# asp.net fine-uploader

我正在尝试使用Fine Uploader来在我们的网站上实现它。 我真的很喜欢分块和恢复功能,但是我遇到了一些将文件放回服务器端的问题;我这样做后他们就腐败了。 经过一些调查后,我发现每个块都是194字节太大,这使得生成的文件大小为194字节。 这是一个已知的问题吗?如果需要,我会发布我的代码。 谢谢你的时间。

编辑这是我的sscce。我忘了指定我正在使用ASP.NET C#。

在网页上初始化上传者

    $(document).ready(function () {
    var manualuploader = new qq.FineUploader({
        element: $('#fine-uploader')[0],
        request: {
            endpoint: 'UploadHandler.ashx',
            forceMultipart: true            
        },        
        chunking: {
            enabled: true
        },
        resume: {
            enabled: true
        },
        retry: {
            enableAuto: true
        },
        callbacks: {
            onSubmit: function (id, fileName) {
                document.getElementById('triggerUpload').style.visibility = 'visible';
            }            
        }

    });
});

服务器端处理程序(c#):

<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.Web;

public class UploadHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
    private int completed;

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;

        string partIndex = request.Params["qqpartindex"];
        int totalParts = Convert.ToInt32(request.Params["qqtotalparts"]);        
        String filename = request.Params["qqfilename"];
        String totalFileSizeName = request.Params["qqtotalfilesize"];

        string uploadedTemp = context.Server.MapPath("~/App_Data/" + "TEMP/");
        string uploadedLocation = context.Server.MapPath("~/App_Data/");

        string filePath = System.IO.Path.Combine(uploadedTemp, partIndex + ".tmp");

        if (!System.IO.File.Exists(filePath))
        {
            System.IO.Stream inputStream = request.InputStream;

            using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(filePath))
            {
                inputStream.CopyTo(fileStream);
            }
        }
        completed = 0;

        if (partIndex.Equals(Convert.ToString(totalParts - 1))) // all chunks have arrived
        {
            mergeTempFiles(uploadedTemp, uploadedLocation, filename);
            completed = 1;

        }       
        context.Response.ContentType = "application/json";
        context.Response.Write("{\"success\":true, \"completed\": " + completed +"}");

    }

    public bool IsReusable
    {
        get { return true; }
    }

    public void mergeTempFiles(string pathOrigin, string pathToSave, string filename)
    {
        string[] tmpfiles = System.IO.Directory.GetFiles(pathOrigin, "*.tmp");

        if (!System.IO.Directory.Exists(pathToSave))
        {
            System.IO.Directory.CreateDirectory(pathToSave);
        }
        System.IO.FileStream outPutFile = new System.IO.FileStream(pathToSave + filename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        foreach (string tempFile in tmpfiles)
        {   
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            System.IO.FileStream inputTempFile = new System.IO.FileStream(tempFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
            while ((bytesRead = inputTempFile.Read(buffer, 0, 1024)) > 0)
                outPutFile.Write(buffer, 0, bytesRead);
            inputTempFile.Close();
            //System.IO.File.Delete(tempFile);
        }
        outPutFile.Close();
    }    
}

1 个答案:

答案 0 :(得分:2)

问题出在我解析来自请求对象的各个块的输入流(在我的c#处理程序类中)的方式,

我读它的方式:

System.IO.Stream inputStream = request.InputStream;

应该阅读的方式:

System.IO.Stream inputStream = request.Files[0].InputStream;

This google groups post建议第二种方式只能在IE中完成,这是所有其他浏览器中的第一种方式,但我发现它在所有浏览器中都是这样的。