通过http传输二进制文件出错

本文关键字:出错 二进制文件 传输 http 通过 | 更新日期: 2023-09-27 18:18:55

我正在做下面的操作来上传一个。pptx文件并将其保存在服务器的磁盘上。

当我试图打开文件时,它说它已损坏,无法打开。

这是因为我的编码类型还是别的什么?

是否有可能在POST中发送任何任意文件作为二进制文件,并使其在一块到达服务器?

    public static void ListenerCallback(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);
        HttpListenerRequest request = context.Request;
        HttpListenerResponse response = context.Response;
        StreamReader reader = new StreamReader(request.InputStream);
        var res = reader.ReadToEnd();
        reader.Close();
        toLog.Add(res);
        NameValueCollection coll = HttpUtility.ParseQueryString(res);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(res);
        File.WriteAllBytes("output.pptx", bytes);
        response.StatusCode = 200;
        response.ContentType = "text/html";
        using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
            writer.WriteLine("File Uploaded");
        response.Close();
    }

这是我要发送的邮差预览:

POST  HTTP/1.1
Host: localhost:80
Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
Cache-Control: no-cache
Postman-Token: 9a04aa33-cd44-d238-6873-b330524f4ae5
undefined

如果我在np++中打开源文件(未损坏),我看到编辑器选择用ANSI编码。

我目前在UTF8编码,我似乎没有选择在ANSI编码。

另外,在比较文件大小时,我丢失了1千字节的数据。

编辑:我的当前代码现在是这个

将一个空文件写入磁盘。

    public static void ListenerCallback(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);
        HttpListenerRequest request = context.Request;
        HttpListenerResponse response = context.Response;
        StreamReader reader = new StreamReader(request.InputStream);
        var res = reader.ReadToEnd();
        reader.Close();
        Console.WriteLine(res);
        NameValueCollection coll = HttpUtility.ParseQueryString(res);
        using (var outp = File.OpenWrite("output.pptx"))
        {
            request.InputStream.CopyTo(outp);
        }
        response.StatusCode = 200;
        response.ContentType = "text/html";
        using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF7))
            writer.WriteLine("File Uploaded");
        response.Close();
    }

通过http传输二进制文件出错

我现在没有时间测试这个,但是试试这个:

public static void ListenerCallback(IAsyncResult result)
{
    HttpListener listener = (HttpListener)result.AsyncState;
    HttpListenerContext context = listener.EndGetContext(result);
    HttpListenerRequest request = context.Request;
    HttpListenerResponse response = context.Response;
    using (var out = File.OpenWrite("output.pptx"))
    {
        request.InputStream.CopyTo(out);
    }
    response.StatusCode = 200;
    response.ContentType = "text/html";
    using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
        writer.WriteLine("File Uploaded");
    response.Close();
}

我认为这是因为您发送的MIME类型是"text/html",而不是正确的类型"application/vnd.openxmlformats-officedocument.presentationml.presentation"。