Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I have an issue to save the pdf file from Page to server --> Application Folder.I have tried, file could not save in server folder

Pls advice me
Thank you

What I have tried:

Private Async Function Downloadbill() As Task

      Dim client As New HttpClient()
      Dim request As New HttpRequestMessage(HttpMethod.Post, "")
      request.Headers.Add("Authorization", "XXXXXXXXXxxxxx")

      Dim payload As String = "{ " & vbCrLf &
          " ""BillType"" : """ & "Parent Invoice" & """," & vbCrLf &
          " ""Bills"" : """ & Me.txtEncode.Text & """," & vbCrLf &
          " ""PrintOption"" : """ & "WB" & """ " & vbCrLf &

      Dim content As New StringContent(payload, Nothing, "application/json")
      request.Content = content

      Dim response As HttpResponseMessage = Await client.SendAsync(request)

      ' Add the Content-Type header to the response
      response.Content.Headers.ContentType = New MediaTypeHeaderValue("application/pdf")

      ' Read the response content as a byte array
      Dim responseBytes As Byte() = Await response.Content.ReadAsByteArrayAsync()

      ' Save the response as a PDF file
      Dim filePath As String = HttpContext.Current.Server.MapPath("../Doc_Uploads/Waybill.pdf")

      File.WriteAllBytes(filePath, responseBytes)

      Console.WriteLine("PDF saved successfully.")

  End Function
Updated 2-Apr-23 20:11pm
OriginalGriff 3-Apr-23 2:06am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

So tell us what it does that you didn't expect, or doesn't do that you did.
Tell us what you tried to fix it.
Tell us any error messages, and where and when they occur.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Thinking about it, two things do occur:
1) Why are you using a hardcoded file path:
Dim filePath As String = HttpContext.Current.Server.MapPath("../Doc_Uploads/Waybill.pdf")
Web apps and inherently multiuser: using a hardcoded file path will mean that all users will overwrite the same file.

2) That path is probably your problem: websites do not run under your user ID, the run under a special ID for IIS which has (deliberately) very restricted access to the rest of the system - so unless that path is fully opened for read and write permissions to pretty much all users the write attempt will fail in production.
Share this answer
Maideen Abdul Kader 3-Apr-23 21:29pm    
hi Griff
Senario: client need download their invoices using api url provided by company which is in pdf format. they have save into client's server . But I have tried to store in application folder (../Doc_Uploads) for using future reference. The folder has setup write/read permission.
the filename is based on invoice number.
But, could not store/save into particular folder.
Pls advice me where i did wrong.

Thank you
OriginalGriff 4-Apr-23 2:35am    
How can I tell without access to your system?
Your file name isn't based on an invoice number, it's a fixed name "Waybill.pdf" which means it's shared by every user.

If you aren't using the code you show us, then we can't fix the code you are using!

Use the debugger: start finding out what is happening on your system - look at error codes and so forth to start with.

Sorry, but we can't do that for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900