简体   繁体   中英

Download Zip file PushStreamContent Javascript

i'am asking the about the right way to downlaod a PushStreamContent present in Post Request , i already preapred the backend request , something like this

private static HttpClient Client { get; } = new HttpClient();
public HttpResponseMessage Get()

{

var filenamesAndUrls = new Dictionary<string, string>
{
    { 'README.md', 'https://raw.githubusercontent.com/StephenClearyExamples/AsyncDynamicZip/master/README.md' },
    { '.gitignore', 'https://raw.githubusercontent.com/StephenClearyExamples/AsyncDynamicZip/master/.gitignore'},
};

var result = new HttpResponseMessage(HttpStatusCode.OK)
{
    Content = new PushStreamContent(async (outputStream, httpContext, transportContext) =>
    {
        using (var zipStream = new ZipOutputStream(outputStream))
        {
            foreach (var kvp in filenamesAndUrls)
            {
                zipStream.PutNextEntry(kvp.Key);
                using (var stream = await Client.GetStreamAsync(kvp.Value))
                    await stream.CopyToAsync(zipStream);
            }
        }
    }),
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "MyZipfile.zip" };
return result;
}

and in the front part , i used axios to send Post request and with the result i create blob to download it (i modified the backend to support Post) but the download take much time and i think this a wrong way to use PushStreamContent and i should use EventSource or something like this.

Thank you.

After few days of search , there are two solution :

  • Change the download request to Get Request instead of Post.
  • Use Fetch instead of axios http request and with the response send it to streamsaver package , it's really amazing and instantly start the download on the fly.

I agree with houssem about changing it to a get request.
I'm the creator of StreamSaver and occasionally i search for ppl talking about it and help someone in need. I often have to tell ppl that it's better to use the server to save files rather than using StreamSaver. StreamSaver is meant for client generated content (good for stuff like WebTorrent or webcam recording)


Download can only happen when you navigate the resource. That means you can't use ajax (xhr, fetch, axios, etc) to trigger a download
a <a href> , <iframe> , location.href = all works fine, but if you really need it to be a post request, then you can also submit a <form> but you will have to do it with application/multipart or URLEncoded and not with a json request or anything else. the con of this is that you can't use custom request headers like a authentication header (unless you use service worker to add them) in such cases it is better with cookies that gets sent along every request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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