Hi
I am getting above error on below code
C#
MemoryStream ms = new MemoryStream(buffer);
FileStream file = new FileStream(@"D:\" + pdf + ".pdf", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();Thanks

Pasang TamangRank #449 · 5.6K reputationPosted Dec 26, 2022, 11:57 PM EST
Hi Ramco,
It's difficult to point out what problem exactly without having complete code. As I see something must be wrong in your pdf variable which you are using for filename. Below code works with me
var pdf = DateTime.Now.ToString("yyyyMMddHHmm"); var test = "Some pdf content goes here"; byte[] buffer = Encoding.ASCII.GetBytes(test); MemoryStream ms = new MemoryStream(buffer); FileStream file = new FileStream(@"D:\" + pdf + ".pdf", FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close();Regards,
Pasang
Shervin CyrilRank #573 · 4.2K reputationPosted Dec 27, 2022, 12:50 AM EST
Your code doent generated any error for me.
As @Pasang said , the issue might be in your file name variable - pdf
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 12:53 AM EST
Hi
I am trying below code
dynamic json = JsonConvert.DeserializeObject(response.Content); string pdf = json.data; byte[] buffer = client.DownloadData(request); MemoryStream ms = new MemoryStream(buffer); FileStream file = new FileStream(@"D:\" + pdf + ".pdf", FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close();Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 12:55 AM EST
Hi
Below is returned - https://nimubs-assets.s3.amazonaws.com/manifest/20221227112426-90.pdf
string pdf = json.data;
byte[] buffer = client.DownloadData(request);
MemoryStream ms = new MemoryStream(buffer);
FileStream file = new FileStream(@"D:\" + pdf , FileMode.Create, FileAccess.Write);
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 12:56 AM EST
Hi
Can we do the same thru RestSharp. Below is returned in pdf
https://nimubs-assets.s3.amazonaws.com/manifest/20221227113803-73.pdf
Thanks
Shervin CyrilRank #573 · 4.2K reputationPosted Dec 27, 2022, 1:15 AM EST
we cannot have certain characters in the full PathFile string.
For example, below characters will crash the StreamWriter function
/ , :
Pasang TamangRank #449 · 5.6K reputationPosted Dec 27, 2022, 1:24 AM EST
Hi,
You can try with below code also
using (var client = new HttpClient())
{
var pdf = DateTime.Now.ToString("yyyyMMddHHmm");
client.BaseAddress = new Uri("https://nimubs-assets.s3.amazonaws.com");
var content = client.GetByteArrayAsync("/manifest/20221227112426-90.pdf");
MemoryStream ms = new MemoryStream(content.Result);
FileStream file = new FileStream(@"D:\" + pdf + ".pdf", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();
}
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 1:37 AM EST
Hi Pasang
Can't we do thru Rest Sharp
Thanks
Pasang TamangRank #449 · 5.6K reputationPosted Dec 27, 2022, 1:48 AM EST
Hi,
Yes, you can do with RestSharp also. Below is the code example with RestSharp
var pdf = DateTime.Now.ToString("yyyyMMddHHmm"); var request = new RestRequest("/manifest/20221227113803-73.pdf", Method.Get); var client = new RestClient("https://nimubs-assets.s3.amazonaws.com"); var content = client.DownloadData(request); MemoryStream ms = new MemoryStream(content); FileStream file = new FileStream(@"D:\" + pdf + ".pdf", FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close();Regards,
Pasang
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 1:57 AM EST
Hi Pasand
This has to be dynamic - 20221227113803-73.pdf. How i can get this from Api returned content.
It is not fixed.
Thanks
Pasang TamangRank #449 · 5.6K reputationPosted Dec 27, 2022, 2:03 AM EST
Hi,
Dynamic filename depends on your case. May be there is different endpoint that gives you the online file URL.
Regards,
Pasang
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 27, 2022, 2:10 AM EST
Hi Pasanf
I think this part will be fixed .- https://nimubs-assets.s3.amazonaws.com/manifest/
This will be dynamic -20221227113803-73.pdf
Thanks
Pasang TamangRank #449 · 5.6K reputationPosted Dec 27, 2022, 2:15 AM EST
Hi,
You are right. Only the PDF filename part is dynamic I think. For dynamic PDF filename, I would suggest to check your API documentation. I can not tell you how that filename dynamic is handled without knowing your API flow.
Regards,
Pasang