I'm using an API which, given a url, redirects to a file on the server. The file names have "_s,_m and _l" appended to the end (small, medium, large). However, since the url's querystring is parsed dynamically, I don't retrieve the actual file name. The image displays correctly, but is it possible to retrieve the filename of the image file from the code? (i.e. where the url has redirected to)?
e.g. http://api.somesite.com/getimage?small (this is what I enter)
"http://somesite.com/images/userimage_s.png" (this is where it redirects to. I would like to get this address from code)
Thanks for any advice
Sounds to me like you are trying to access some images you shouldn't access programmatically ;-)
You could access the given URL with an HTTP client (opening the stream with a stream reader might already suffice) and watch out for a Location header, which will most likely contain the URL you are searching for.
Related
I have this image URL i got from database.
\\Imagepath\ImageFolder\image.png
but i need to put in a img tag in html to show the image in the page, i try to do this way
<img src='\\Imagepath\ImageFolder\image.png'/>
but the page adds the default url for localhost, ie.
http://localhost:1234/\\Imagepath\ImageFolder\image.png
I need your helpfor trying to get the image from that server URL.
I have a lot of images around 6,200 so is not an option to download, i'm show the images in a table.
NOTE: i know the img tag not accept the URL like i have, but maybe you have an idea to do in ASP.NET, i apretiate your help.
NOTE 2:i'm using ASP.NET MVC.
Paths starting with \\ are UNC paths, they are not URLs. In a browser, you have to use a URL to load an image.
The browser is assuming you've tried to specify a relative URL, and is attempting to add the current URL by default in order to fully qualify it and then make a request to it to get the image.
You need to map the path to a virtual directory in your webserver and then point the image's src property at the URL of that virtual directory.
Alternatively, if that's not a workable solution you could write an MVC action method which takes the name of the image file as a parameter and then loads the image from the UNC path in the background and returns the data in a binary response to the browser.
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 have HTTP Handler where I want to log the response. I know there is option context.Request.SaveAs(filename... But I never tried this before. How can I use this, I mean someone can be more specific about file name?
Well, the documentation is pretty straight forward about this:
The [SaveAs] call specifies that the request be saved as a text file
in a directory where the ASP.NET process identity has been granted
write permissions, and that any header information included in the
request is included in the file.
You can thus simply save the entire HttpRequest (including the headers in a file). You can simply determine the path and a boolean indicating whether you want to incude the headers as well:
context.Request.SaveAs(#"c://myLogFile.log",true)
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.
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.