当所有块在精细上传器中成功上传时,响应将变为空白

时间:2014-11-25 08:51:48

标签: fine-uploader

当所有块在精细上传程序中成功上传时,响应将变为空白。

发生错误如下:

[Fine Uploader 5.0.8]文件0的块13成功上传。

[Fine Uploader 5.0.8]所有块都已上传为0 - 最终确定....

[Fine Uploader 5.0.8]尝试解析xhr响应文本时出错(xhr未定义)

我的代码是:

public class FineUploader : IHttpHandler
{
    protected Log4NetLogger logger = new Log4NetLogger();
    private int completed;
    string[] tmpfiles;
    string encryptedFileName = "";
    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;

        string page = "";
        string savepath = "/PUploads/Docs";
        string requestpath = "/PUploads/Requests";
        string uploadedLocation = context.Server.MapPath(savepath);
        string uploadedLocationRequest = context.Server.MapPath(requestpath);
        string qquuid = request.Params["qquuid"];
        string partIndex = request.Params["qqpartindex"];
        int totalParts = Convert.ToInt32(request.Params["qqtotalparts"]);
        String filename = request.Params["qqfilename"];
        String totalFileSizeName = request.Params["qqtotalfilesize"];
        decimal filesize = Convert.ToDecimal(request.Params["qqtotalfilesize"]);
        if (context.Request.Form["Page"] != null && context.Request.Form["Page"] != "")
        {
            page = context.Request.Form["Page"];
        }
        if (filesize <= 3000000)
        {
            HttpPostedFile postedFile = context.Request.Files[0];
            encryptedFileName = Utils.GetEncryptedFileName(Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename));
            if (!Directory.Exists(uploadedLocation))
                Directory.CreateDirectory(uploadedLocation);
            if (page == "Request")
            {
                uploadedLocation = uploadedLocationRequest;
            }

            postedFile.SaveAs(uploadedLocation + @"\" + encryptedFileName);
            if (page == "ClientUpload")
            {
                ClientDocuments(context, encryptedFileName, filename, filesize / 1024);
            }
        }
        else
        {
            string uploadedTemp = context.Server.MapPath("~/Uploads/" + "Temp/" + qquuid);
            if (!Directory.Exists(uploadedTemp))
                Directory.CreateDirectory(uploadedTemp);

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

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

                using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(filePath))
                {
                    inputStream.CopyTo(fileStream);
                }
            }
            completed = 0;
            tmpfiles = System.IO.Directory.GetFiles(uploadedTemp, "*.tmp");
            if (partIndex.Equals(Convert.ToInt32(totalParts - 1))) // all chunks have arrived
            {
                encryptedFileName = Utils.GetEncryptedFileName(Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename));
                if (page == "Request")
                {
                    uploadedLocation = uploadedLocationRequest;
                }

                mergeTempFiles(uploadedTemp, uploadedLocation, encryptedFileName);


                if (page == "ClientUpload")
                {
                    ClientDocuments(context, encryptedFileName, filename, filesize / 1024);
                }
                completed = 1;

            }
        }

            FileResponse e = new FileResponse();
            e.success = true;
            e.completed = completed;
            e.filename = encryptedFileName;
            e.filesize = filesize / 1024;

            context.Response.ContentType = "application/json";
            context.Response.StatusCode = 200;
            var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString = javaScriptSerializer.Serialize(e);
            context.Response.Write(jsonString);


    }
    public void mergeTempFiles(string pathOrigin, string pathToSave, string filename)
    {
        tmpfiles = System.IO.Directory.GetFiles(pathOrigin, "*.tmp");
        logger.Debug("merged file count" + tmpfiles.Count().ToString());
        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);

        }

        System.IO.Directory.Delete(pathOrigin);
        outPutFile.Close();
    }

    public void ClientDocuments(HttpContext context, string encryptedfilename, string Originalfilename, decimal filesize)
    {

        long? clientid = null;
        long? clientsubuserid = null;
        long? firmid = null;
        string modifiedby = null;
        string name = "";
        string desc = "";
        if (context.Request.Form["name"] != null && context.Request.Form["name"] != "")
        {
            name =context.Request.Form["name"];
        }
        if (context.Request.Form["description"] != null && context.Request.Form["description"] != "")
        {
            desc = context.Request.Form["description"];
        }
        if (context.Request.Form["FirmId"] != null && context.Request.Form["FirmId"] != "")
        {
            firmid = Convert.ToInt64(context.Request.Form["FirmId"]);
        }
        if (context.Request.Form["ClientId"] != null && context.Request.Form["ClientId"] != "")
        {
            clientid = Convert.ToInt64(context.Request.Form["ClientId"]);
        }
        if (context.Request.Form["ClientSubUserId"] != null && context.Request.Form["ClientSubUserId"] != "")
        {
            clientsubuserid = Convert.ToInt64(context.Request.Form["ClientSubUserId"]);
        }
        if (context.Request.Form["ModifiedBy"] != null && context.Request.Form["ModifiedBy"] != "")
        {
            modifiedby = context.Request.Form["ModifiedBy"];
        }
        DocumentViewService doc = new DocumentViewService();
        doc.InsertClientDocument(encryptedfilename, Originalfilename, filesize, name, desc, firmid, clientid, clientsubuserid, modifiedby);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

public class FileResponse
{
    public bool success { get; set; }
    public string filename { get; set; }
    public decimal filesize { get; set; }
    public int completed { get; set; }
}

0 个答案:

没有答案