Michael1834
Michael1834

Reputation: 149

download file on the user's side

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

Upvotes: 1

Views: 742

Answers (2)

Michael1834
Michael1834

Reputation: 149

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");

Upvotes: 1

Peter Kolos
Peter Kolos

Reputation: 46

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"));

Upvotes: 1

Related Questions