简体   繁体   English

使用C#中的Post下载附件文件

[英]Download Attachment file using Post in c#

I am trying to download a xlsx-attachment using a post-request but don't have a clue how to recieve the xlsx file. 我正在尝试使用请求后下载xlsx附件,但不知道如何接收xlsx文件。 I found several different solutions which used a get-request which is not applicable in this case since i have to send data to the server which creates the file specificqally to the sent filters. 我发现了几种使用get-request的不同解决方案,该解决方案在这种情况下不适用,因为我必须将数据发送到服务器,该服务器专门为发送的过滤器创建文件。

The following is the current situation: 以下是当前情况:
1. I send a Post request with several Parameters 1.我发送带有几个参数的发布请求
2. The server generates a xlsx file based on the parameters of the post request 2.服务器根据发布请求的参数生成一个xlsx文件
3. I receive the data (but i dont know how to obtain the xlsx out of the data) 3.我收到数据(但是我不知道如何从数据中获取xlsx)

The data has to be somewhere as shown in the screenshot below but neigther in the response header, nor in the paramenters or the response content I can find data which could be a file. 数据必须位于下面的屏幕快照中所示的某个位置,但必须位于响应标头中,而不是参数或响应内容中,我可以找到可能是文件的数据。
在此处输入图片说明

This is the Header of the response: 这是响应的标题:

Request URL:https://www.example.com/en/list.xlsx
Request Method:POST
Status Code:200 
Remote Address:104.25.95.108:443
Referrer Policy:no-referrer-when-downgrade

Response Headers
cache-control:max-age=0, private, no-store, no-cache, must-revalidate
cf-ray:38a32289090a26d8-FRA
content-disposition:attachment; filename="secondary.xlsx"
content-length:2017481
content-type:application/vnd.openxmlformats
officedocument.spreadsheetml.sheet; name=secondary.xlsx
date:Sun, 06 Aug 2017 15:47:40 GMT
expires:Sun, 06 Aug 2017 15:47:28 GMT
pragma:public
server:cloudflare-nginx
set-cookie:alive=1; expires=Sun, 06-Aug-2017 17:47:40 GMT; Max-Age=7200; path=/
status:200
strict-transport-security:max-age=63072000
x-content-type-options:nosniff
x-content-type-options:nosniff
x-frame-options:sameorigin
x-frame-options:DENY

this is my current code: 这是我当前的代码:

public static string POST(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
    {
        var content = new FormUrlEncodedContent(values);
        var response = GlobalVar.client.PostAsync(GlobalVar.baseURI + extendURI, content).Result;
        string responseString = response.Content.ReadAsStringAsync().Result;
        return responseString;
    }

EDIT: Solution: get the system.io.stream and write to file 编辑:解决方案:获取system.io.stream并写入文件

public static string Download_XML(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
    {
        var content = new FormUrlEncodedContent(values);
        var response = GlobalVar.client.PostAsync(GlobalVar.mintosURI + extendURI, content).Result;

        string responseString = response.Content.ReadAsStreamAsync().Result.ToString();
        Stream stream = response.Content.ReadAsStreamAsync().Result;
        using (FileStream file = new FileStream("testfile.xlsx", FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);
            file.Write(bytes, 0, bytes.Length);
            stream.Close();
        }
        //string responseString = response.Result.Content.ReadAsStringAsync().ToString();
        return responseString;
    }

try this 尝试这个

HttpResponseMessage finalResponse = Request.CreateResponse();

 finalResponse.Headers.AcceptRanges.Add("bytes");
    finalResponse.StatusCode = HttpStatusCode.OK;
    finalResponse.Content = new StreamContent(response);
    finalResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("render");
    finalResponse.Content.Headers.ContentDisposition.FileName = "fileName";
    finalResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("<set media type>");
    finalResponse.Content.Headers.ContentLength = response .Length;

return finalResponse;

Dont forget to safe your code against network failures 不要忘记保护您的代码以防网络故障

public static void Download_File_Post(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
{
    //create post parameters
    var content = new FormUrlEncodedContent(values);
    var response = GlobalVar.client.PostAsync(GlobalVar.baseURI + extendURI, content).Result;

    Stream stream = response.Content.ReadAsStreamAsync().Result;
        using (FileStream file = new FileStream("testfile.xlsx", FileMode.Create, System.IO.FileAccess.Write))
    {
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes, 0, (int)stream.Length);
        file.Write(bytes, 0, bytes.Length);
        stream.Close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM