C# Plupload、C、FTP和分块

C# Plupload、C、FTP和分块,c#,asp.net,plupload,ftpwebrequest,chunking,C#,Asp.net,Plupload,Ftpwebrequest,Chunking,我使用Plupload来管理大文件上传。我已经编写了一个C处理程序,使用FTPWebRequest上传,它似乎工作正常。问题是我想使用分块。启用分块时,我会将多个文件上载到名为blobxxx、blobyyyy的FTP服务器。问题是,最后我如何将它们连接在一起?以下是我的代码-感谢您的帮助: Plupload.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="plupload.aspx.cs" Inherits=

我使用Plupload来管理大文件上传。我已经编写了一个C处理程序,使用FTPWebRequest上传,它似乎工作正常。问题是我想使用分块。启用分块时,我会将多个文件上载到名为blobxxx、blobyyyy的FTP服务器。问题是,最后我如何将它们连接在一起?以下是我的代码-感谢您的帮助:

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>
致:


它似乎上传正常。只需测试分块…

您的链接有一个标题为“服务器端处理”的部分,其中描述了您需要自己重新组装文件。以下是有关附加二进制文件的信息的链接:


Plupload文档提到,他们在ChunkUpload事件上添加内容,并在文件到达时将其拼凑在一起。

是的,我看到过类似的链接。我想,当您的文件与处理程序位于同一服务器上时,这也没关系。但是它如何通过FTP工作,有凭证吗?@Captain_Planet啊,我错过了那部分。那么,您使用Plupload将单个块发送到FTP服务器?是的,就是这样。我有客户端、web服务器和FTP服务器。客户上传的一些内容可能会更大,因此如果我能将其分块,我想这会更好……@Captain_Planet非常有趣。在您的情况下,我认为您必须在FTP服务器上安装一些程序来监控文件,并在文件到达时重新组装它们。也许你可以使用FTP的自动恢复功能来代替分块,我对它不太了解,但它可能会起作用……谢谢迈克。是的,这是个难题。我会睡一觉的…:-
<%@ 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();

            }
        }
    }


}
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;