客户端/服务器发送大文件

时间:2015-11-16 14:31:11

标签: c# .net tcp tcplistener httplistener

我即将编写一个服务器应用程序,它应该能够从多个来源接收大文件(像所有其他FTP客户端/服务器应用程序一样安静)。

但我不确定什么是最好的方法,需要一些建议。

客户端会将XML数据发送到服务器,如下所示:

<Data xmlns="http://schemas.datacontract.org/2004/07/DataFiles" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Category>General</Category>
    <Files>
        <DataFile>
            <Extension>.txt</Extension>
            <Filename>test4</Filename>
            <Bytes>"Some binary data"</Bytes>
        </DataFile>
    </Files>
</Data>

我开始创建一个 HTTPListener 作为我的服务器,但它似乎在服务器端的大文件上很多(基本上,因为上下文是作为一个数据包未被分段收到的,并且当服务器会对收到的XML数据进行反序列化,然后将其加载到内存中,这对于大型文件来说是不可取的。

然后我转到 TcpListener 以更低层,这似乎在大文件上工作正常,因为它们被发送碎片,但让我做很多工作来追加收到请求时,在服务器端打包。

我很快就在 WCF 上移动了,但是我对这项技术的经验不足,让我再次离开了这个方法。

您会做什么?您将使用哪个.NET工具箱中的.NET工具来创建FTP服务器/客户端?

关于TcpListeners等有很多线程,这不是我在这里寻求的。我需要建议我应该采用哪种方法,以及最佳实践。

编辑: 忘了提到它背后的想法更像是一个FTP代理(客户端将文件发送到服务器&gt;本地服务器存储文件&gt;服务器将其发送到第三方位置&gt;服务器在将文件发送到第三方时清除本地存储的文件位置成功完成。)

编辑17-11-15:

以下是我如何使用HTTP服务器的示例代码:

public class HttpServer
{
    protected readonly HttpListener HttpListener = new HttpListener();

    protected HttpServer(IEnumerable<string> prefixes)
    {
        HttpListener.Prefixes.Add(prefix);
    }

    public void Start()
    {
        while (HttpListener.IsListening && Running)
        {
            var result = HttpListener.BeginGetContext(ContextReceived, HttpListener);
            if (WaitHandle.WaitAny(new[] {result.AsyncWaitHandle, _shutdown}) == 0)
                return;
        }
    }

    protected object ReadRequest(HttpListenerRequest request)
    {
        using (var input = request.InputStream)
        using (var reader = new StreamReader(input, request.ContentEncoding))
        {
            var data = reader.ReadToEnd();
            return data;
        }
    }

    protected void ContextReceived(IAsyncResult ar)
    {
        HttpListenerContext context = null;
        HttpListenerResponse response = null;
        try
        {
            var listener = ar.AsyncState as HttpListener;
            if (listener == null) throw new InvalidCastException("ar");
            context = listener.EndGetContext(ar);
            response = context.Response;
            switch (context.Request.HttpMethod)
            {
                case WebRequestMethods.Http.Post:
                    // Parsing XML data with file at LARGE byte[] as one of the parameter, seems to struggle here...
                    break;
                default:
                    //Send MethodNotAllowed response..
                    break;
            }
            response.Close();
        }
        catch(Exception ex)
        {
            //Do some properly exception handling!!
        }
        finally
        {
            if (context != null)
            {
                context.Response.Close();
            }
            if (response != null)
                response.Close();
        }
    }
}

客户正在使用:

using (var client = new WebClient())
{
    GetExtensionHeaders(client.Headers);
    client.Encoding = Encoding.UTF8;
    client.UploadFileAsync(host, fileDialog.FileName ?? "Test");
    client.UploadFileCompleted += ClientOnUploadFileCompleted;
    client.UploadProgressChanged += ClientOnUploadProgressChanged;
}

请注意,客户端应该将数据(作为XML)发送到服务器,服务器将对接收到的数据进行反序列化(使用文件流服务器端),如前所述。

这是我的TcpServer示例:

public class TcpServer
{
    protected TcpListener Listener;
    private bool _running;

    public TcpServer(int port)
    {
        Listener = new TcpListener(IPAddress.Any, port);
        Console.WriteLine("Listener started @ {0}:{1}", ((IPEndPoint)Listener.LocalEndpoint).Address, ((IPEndPoint)Listener.LocalEndpoint).Port);
        _running = true;
    }

    protected readonly ManualResetEvent TcpClientConnected = new ManualResetEvent(false);
    public void Start()
    {
        while (_running)
        {
            TcpClientConnected.Reset();
            Listener.BeginAcceptTcpClient(AcceptTcpClientCallback, Listener);
            TcpClientConnected.WaitOne(TimeSpan.FromSeconds(5));
        }
    }

    protected void AcceptTcpClientCallback(IAsyncResult ar)
    {
        try
        {
            var listener = ar.AsyncState as TcpListener;
            if (listener == null) return;

            using (var client = listener.EndAcceptTcpClient(ar))
            {
                using (var stream = client.GetStream())
                {
                    //Append or create to file stream
                }
            }

            //Parse XML data received?
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            TcpClientConnected.Set();
        }
    }
}   

1 个答案:

答案 0 :(得分:2)

创建一个新的空MVC应用程序

enter image description here

接下来,将新控制器添加到Controllers文件夹

using System.Web;
using System.Web.Mvc;

namespace UploadExample.Controllers
{
    public class UploadController : Controller
    {
        public ActionResult File(HttpPostedFileBase file)
        {
            file.SaveAs(@"c:\FilePath\" + file.FileName);
        }

    }
}

现在,您只需将上传文档作为多部分表单数据发布到您的网站...

void Main()
{   
    string fileName = @"C:\Test\image.jpg";
    string uri = @"http://localhost/Upload/File";
    string contentType = "image/jpeg";

    Http.Upload(uri, fileName, contentType);
}

public static class Http
{
    public static void Upload(string uri, string filePath, string contentType)
    {
        string boundary         = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundaryBytes    = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        string formdataTemplate = "Content-Disposition: form-data; name=file; filename=\"{0}\";\r\nContent-Type: {1}\r\n\r\n";
        string formitem         = string.Format(formdataTemplate, Path.GetFileName(filePath), contentType);
        byte[] formBytes        = Encoding.UTF8.GetBytes(formitem);

        HttpWebRequest request  = (HttpWebRequest) WebRequest.Create(uri);
        request.KeepAlive       = true;
        request.Method          = "POST";
        request.ContentType     = "multipart/form-data; boundary=" + boundary;
        request.SendChunked     = true;

        using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
            requestStream.Write(formBytes, 0, formBytes.Length);

            byte[] buffer = new byte[1024*4];
            int bytesLeft;

            while ((bytesLeft = fileStream.Read(buffer, 0, buffer.Length)) > 0) requestStream.Write(buffer, 0, bytesLeft);

            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
        }

        using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
        {
        }

        Console.WriteLine ("Success");    
    }
}

修改

如果您遇到问题,请编辑您的Web.Config文件,这可能是您达到了请求长度限制......

<system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"  maxRequestLength="1048576"/>
</system.web>

我错过的另一件事(但现在已经编辑过)是webrequest上的send chunked属性。

request.SendChunked = true;