I want some external client to search in one specific directory of my ASP.NET website for a specific file, and that file should be generated (and given to the client) when the user makes a request.
Is it possible? Which method should I use to program this response?
Yes, you could build an ASPX page for example that created the file on Page_Load and then set the byte[] of that file to the Response of the page.
... Page_Load(...)
{
// create the file
// set the response
Response.WriteFile(pathToNewFile);
}
Related
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
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.
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 am building a system which sells pdf's to our customers. We have a 3rd party which creates and manages the pdf's. When a purchase is made, our api calls their api which returns a byte[].
How do I start a download from this?
Example process:
User clicks Purchase
Javascript makes ajax call -> /api/PurchaseProduct/product/information
C# then calls 3rd party api in similar fashion
Return is a byte[] - Which is the document
From here I don't know if the byte[] should be returned to the browser or if something else should happen. If I return the byte[] does javascript then need to turn it into a pdf? Should C# be turning the byte[] into a pdf and then return that?
I am afraid this is very new and im not sure where to start.
Thanks
Edit
We use ASP.Net MVC and our site is a single page application.
File size varies depending on what is purchased.
There are no way of returning a file to the user via AJAX. The browser has to request the file using a normal, HTTP request.
The best solution would be to save the file to disk after the ajax request, and then return the path to the client so that the file can be opened as usual with e.g. window.open.
In the method, which is called by AJAX, include the following:
Current.Response.ClearHeaders();
Current.Response.ClearContent();
Current.Response.AppendHeader("Content-Disposition", "attachment; filename=file.pdf");
Current.Response.ContentType = "application/pdf";
Current.Response.BinaryWrite(returnedByte);
Current.Response.End();
where returnedByte is the byte[]
I have Flex application requiring to filter users depending on there database groups. Depending on which group they are, the're is a config.xml file that is use to populate the swf.
Here is how I figure how to do this :
1. The client comes to a .aspx page with a form requiring a username and a password.
2. On the server side I confirm the user credential
3. Once the username/password is valid I redirect to the mxml file with the config.xml file in the html headers (post).
My problem comes when I need to get the post data from the http request. Let's say I have this code :
<mx:Application initialize="init()">
<mx:Script>
<![CDATA[
private function init():void
{
// get the post data here
}
/* More code here */
]]>
</mx:Script>
</mx:Application>
How do I get the post data on the init() function.
Thank you.
For those that would be interested, I've found some ressources on the Adobe Flex 3 Ressource center.
Basically there is no current way to pass data with the POST method. You can either add the parameters at the end of you swf url (GET method) as shown here : http://livedocs.adobe.com/flex/3/html/help.html?content=deep_linking_5.html#245869
The other way is to embed them in the page with the flashVars method shown here : http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html#229997
If you still wonder, how I'll manage to do this if you run to in the same situation. Here is my idea (feel free to share if you have different vision) :
1.User logs in login.aspx
2.Depending on the credentials of the users the server side code modify the index.html file to embed the correct xml file in the flash object.
3.With the FlashVars method, I get back the xml file path and job done!
If you ever run in a similar situation and need help contact me.
I don't think it's possible to get the POST data, but others might have a way. An alternative solution would be:
User logs in: login.aspx
User directed to Flash content: content.html embedding content.swf
Flash requests config.xml from server: content.swf makes HTTP request for config.xml.aspx
Server provides user's configuration in config.xml.aspx
In your init() function, you'd make the URLLoader request to get the configuration, and you'd do the configuration in the Event.COMPLETE handler.
Another possibility is to use HTTP cookies--not handled natively by Flash, but you can get to them via Javascript--see this CookieUtil class.