Plupload,C#,FTP和Chunking

时间:2014-10-31 16:48:41

标签: c# asp.net plupload ftpwebrequest chunking

我使用Plupload来管理大型文件上传。我已经编写了一个使用FTPWebRequest上传的C#处理程序,它似乎工作正常。麻烦的是我想使用分块(http://www.plupload.com/docs/Chunking)。当我启用分块时,我将多个文件上传到我的FTP服务器,名为blobxxx,blobyyyy。问题是,我最后如何将它们全部加在一起?这是我的代码 - 感谢您的帮助:

Plupload.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="plupload.aspx.cs" Inherits="plupload" %>

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

<title>Plupload - Custom example</title>

<!-- production -->

    <link href="plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css" rel="stylesheet" type="text/css" />

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" charset="UTF-8"></script>

    <script type="text/javascript" src="plupload/js/plupload.full.min.js"></script>

    <script src="plupload/js/jquery.plupload.queue/jquery.plupload.queue.min.js" type="text/javascript"></script>

<!-- debug 
<script type="text/javascript" src="../js/moxie.js"></script>
<script type="text/javascript" src="../js/plupload.dev.js"></script>
-->

</head>
<body style="font: 13px Verdana; background: #eee; color: #333">

<h1>Custom example</h1>

<p>Shows you how to use the core plupload API.</p>

<div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<br />

<div id="uploader">
    <p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
</div>

<script type="text/javascript">
// Initialize the widget when the DOM is ready
$(function() {
    // Setup html5 version
    $("#uploader").pluploadQueue({
        // General settings
        runtimes : 'html5,flash,silverlight,html4',
        url : "FileUpload.ashx",
        chunk_size: '1mb',
        max_retries: 3,

        rename : true,
        dragdrop: true,


        // Resize images on clientside if we can
        resize: {
            width : 200,
            height : 200,
            quality : 90,
            crop: true // crop to exact dimensions
        },


        // Flash settings
        flash_swf_url : 'plupload/js/Moxie.swf',

        // Silverlight settings
        silverlight_xap_url : 'plupload/js/Moxie.xap'
    });

    $("#uploader").bind("Error", function (upload, error) {
        alert(error.message);
    });

    // only allow 5 files to be uploaded at once
    $("#uploader").bind("FilesAdded", function (up, filesToBeAdded) {
        if (up.files.length > 5) {
            up.files.splice(4, up.files.length - 5);
            showStatus("Only 5 files max are allowed per upload. Extra files removed.", 3000, true);
            return false;
        }
        return true;
    });

});
</script>

</body>
</html>

FileUpload.ashx

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

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

public class FileUpload : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Files.Count > 0)
        {

            for (int a = 0; a <= context.Request.Files.Count - 1; a++) 
            {
                FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));

                clsRequest.Credentials = new NetworkCredential("xxx", "yyy");
                clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;


                Stream streamReader = context.Request.Files[a].InputStream;                
                byte[] bFile = new byte[streamReader.Length];

                streamReader.Read(bFile, 0, (int)streamReader.Length);
                streamReader.Close();
                streamReader.Dispose();

                Stream clsStream = clsRequest.GetRequestStream();
                //clsStream.Write(bFile, 0, bFile.Length);

                // Write the local stream to the FTP stream
                // 2 bytes at a time
                int offset = 0;
                int chunk = (bFile.Length > 2048) ? 2048 : bFile.Length;
                while (offset < bFile.Length)
                {
                    clsStream.Write(bFile, offset, chunk);
                    offset += chunk;
                    chunk = (bFile.Length - offset < chunk) ? (bFile.Length - offset) : chunk;
                }

                clsStream.Close();
                clsStream.Dispose();

                FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();

                response.Close();

            }
        }
    }


}

UPDATE **

我改变了:

FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));

为:

 FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + "staticFileName.zip"));

clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

clsRequest.Method = System.Net.WebRequestMethods.Ftp.AppendFile;

似乎上传确定。需要测试分块...

1 个答案:

答案 0 :(得分:0)

您的链接有一个标题为“服务器端处理”的部分,该部分描述您需要自己重新组合文件。以下是有关附加二进制文件的信息的链接:How append data to a binary file?

Plupload文档提到它们会在ChunkUploaded事件上附加并在文件到达时将文件分组。