I need to be able to download a file from url. The url does not link to an actual file instead it generates the file on the server first and then gives a download dialog. Probably returning an mvc FileResult.
I'm just interested in getting the byte[] from the file.
I've tried:
using (var webClient = new WebClient())
{
System.Uri uri = new System.Uri(Document.Url);
bytes = await webClient.DownloadDataTaskAsync(uri);
}
This works but I get a corrupted file as expected.
I do not have control over how the server generates or serves the file.
Any way to wait for the file to complete generating and then get the file content?
TIA
Never mind. Turns out the link returns some javascript that auto authenticates a user and then does a jquery get to another url and port to generate the file. So I was basically downloading that script and saving it to pdf. doh.
So a work around would be to mimic that in some way.
Related
I have some code that connects to an HTTP API, and is supposed to get an XML response. When the API link is placed in a browser, the browser downloads the XML as a file. However, when the code connects to the same API, HTML is returned. I've told the API owner but they don't think anything is wrong. Is there a way to capture the downloaded file instead of the HTML?
I've tried setting the headers to make my code look like a browser. Also tried using WebRequest instead of WebClient. But nothing works.
Here is the code, the URL works in the browser (file downloaded) but doesn't work for WebClient:
WebClient webClient = new WebClient();
string result = webClient.DownloadString(url);
The code should somehow get the XML file instead of the page HTML (actually the HTML doesn't appear in the browser, only the file).
The uri that you access may be a HTML page that have its own mechanism (like generating the actual download address which may be dynamically generated by the server and redirecting to it, in order to prevent external linking access) to access the real file.
It is supposed to use a browser background browser core like CefSharp to run the HTML and its javascript to let it navigate, and probably you may want to hook the download event to handle the downloading.
I think you need to add accept header to the WebClient object.
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.Accept] = "application/xml;q=1";
string result = webClient.DownloadString(url);
}
Thank you all for your input. In the end, it was caused by our vendor switching to TLS 1.2. I just had to force my code to use 1.2 and then it worked.
I was working on a uwp project which uses webview to display webpages. I want to save a webpage locally from a url. I tried few these how to save the webpage using c#?
But it doesn't seem to be working as the solution is for a basic windows form app.
Please, forgive if I made any mistake asking question as I'm totally new to stackoverflow.
I want to save a webpage locally from a url.
It is similar to windows form. Try to send get request to the URL, and get the buffer or HTML content. For sending get request, you could use HttpClient. And then save the html buffer to a local file which format is .html. For example:
HttpClient httpclient = new HttpClient();
var result = await httpclient.GetBufferAsync(new Uri("https://www.microsoft.com/en-sg/"));
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("test2.html", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(file, result);
After downloaded you may find the downloaded file in C:\Users\{username}\AppData\Local\Packages\{PackageId}\LocalState.
For more details about file operations please reference Files, folders, and libraries.
My web application (C# and ASP.Net) allows someone to upload a jpg/pdf file and save it as a memory stream inside a SQL table. This is what it looks like once written in the table:
0xFFD8FFE000104A4649.....
Now I want to provide this file back on my web interface through a link where the user can download this file. I have retrieved my file by converting the string above back to a byte array using
filedata = Encoding.ASCII.GetBytes(<string above>);
Then I called this:
string filename = "pic.jpg";
File.WriteAllBytes(filename, filedata);
But I have no clue how I should post this back to the user as a downloadable link on my web interface. Do I have to save this file to a temporary folder on my server or is there a way that I could invoke a call to render my file back as a picture where the user will be prompted to save the file or open it?
Thank you!
You will need to create a page / action / function that writes the bits back as the response to an HTTP request. Keep in mind that in doing so you will probably need to set the proper Content-Type header in the response according to what your file is.
So, you generate a link that calls your page / action / function. Then That sends the binary data back in an HTTP response. Something like
pic.jpg
If you some detail as to what framework you're using (MVC, WebForms, etc) then we can give more detailed examples.
Depending on how you want to present the image.
If you want to embed the image in the web return an link to the file with
string filename = Path.Combine(HttpRuntime.AppDomainAppPath, "Content/Images/pic.jpg");
File.WriteAllBytes(filename, filedata);
return filename;
then in the JavaScript side create an img element with thatsrc
Or if you want the user to download the file do what #squillman says in his answer
What's a good method for getting the content of a .aspx web page as a String. This is from a C# Class which is compiled and deployed to the same machine.
Should I use a HTTP request? Or is there a means of doing the same through a file path, and would this trigger the code behind of the page?
If you use HTTP request, like WebClient and use WebClient.DownloadString(#"http://someSite/somepage.aspx"), that would trigger the server side code and you will get HTML generated by the server. Not the actual aspx page.
But if you use File.ReadAllLines('somepage.aspx') from your current project then you will get the file contents and it will not trigger the server side code. But you can only do that from your current project. You can't access an aspx page over http
This code retrieves the root directory that is hosting the application in IIS. Then, it concatenates the filename. Finally, it reads the contents into a string variable.
Reading a file from disk does not trigger the code-behind; only a request through the ASP.NET pipeline does that.
string path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "Index.aspx");
string fileContents = File.ReadAllText(path);
An HTTP request would usually not return the content of the aspx file since the server should interpret it (take this with a grain of salt, it all depends on your server and how its configured, maybe you've got a special setup)
If you want to read the content of the aspx file, open the file using its location on the disk. It won't trigger the code behind the page since this is triggered by the web server and you're not using it using a file path
if you want to read the output produced when the aspx file is read, open the url to this file. It will trigger the backing code and you will received the output.
I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.