Home > other >  download file on the user's side
download file on the user's side

Time:06-24

I'm looking for a way to save the file to the user's files directory

I convert html to pdf, and save pdf to temporary file

    string fileName = System.IO.Path.GetTempPath()   id.ToString()   ".pdf";
    await page.PdfAsync(fileName)

Here I try download file (.pdf) on the user's site

    var client = new WebClient();
    client.DownloadFile(fileName, $"{id}.pdf"); 

but I think it save to the server nothing on the user's side when the app is published is not displayed

CodePudding user response:

I guess you can use the class KnownFolders from here

https://www.codeproject.com/Articles/878605/Getting-All-Special-Folders-in-NET

and then on the client side make something like that

var client = new WebClient();
client.DownloadFile(fileName, Path.Combine(KnownFolders.GetPath(KnownFolder.Downloads), $"{id}.pdf"));

CodePudding user response:

if you want allow the person who will use the application to download the file:

u can use https://github.com/hardkoded/puppeteer-sharp to convert html to .pdf in Stream format

ServiceController

public async Task<Stream> BytesStreamWord(string html)
  { 
    await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
    await using var page = await browser.NewPageAsync();
    await page.SetContentAsync(html);
    var result = await page.GetContentAsync();

    Stream bytesStream = await page.PdfStreamAsync();

   return bytesStream;
}

and use return File() in method

ExportController

    return File(bytesStream, "application/pdf", "sampleName.pdf");
  • Related