C# upload file to website - c#

I am attempting to post a reply to an image board website.
It can be done through a winform web browser control or http request.
The issue is that with a post, you can upload an image with a input type file element on the page
<input type="file" />
For security reasons, I cannot set the value of the element to the file I want to upload.
When I use tamper data to see what is passed to the posting page, this is parameter that is passed under POST_DATA
-----------------------------256672629917035
Content-Disposition: form-data; name="upfile"; filename="image_file_name.jpg"
Content-Type: image/jpeg
So how is it possible to simulate a file upload of a input element in C#?
Look closely at the data that was posted, no directory is specified.

You can simulate any HTTP request through the HttpWebRequest object. Click here for an example.
When you're using the input type file element the browser is creating the request this for you. If you simply recreate this in HttpWebRequest you have full control over all aspects of the request and can modifiy as you wish.
It's probably in your interest to grab a copy of Fiddler.

If I understood your problem right,
you can't determine the directory of the file which was uploaded from your web browser control.
So ... it is impossible. The information about directory isn't given by browser - so it shouldn't be given by web browser control.
As MizardX suggested, it's better to send request using C#.

Related

Html img tag is not showing image from url in asp.net

<img src="http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542">
The above tag shows image when i create notepad html page but when i put it on asp.net it does not show image on the browser.
It doesn't work because the server behind the URL in question checks the HTTP referer and returns an empty page if a referer is set which doesn't match the expected one.
The referer tells the "vclogos" server from which website the request for loading the image came from.
This is probably a security measure from the page because they don't want people to do what you just tried to do: hotlink their images.
The thing is, when you test it from a "notepad html page", no referer is sent. This is also for security reasons, but from your browser's side: Sending file:///C:/Users/JohnDoe/Desktop/MyPrivateFiles/page.html as referer URL would disclose private information from your computer. That's why nothing is sent. Therefore, the image is loaded - the same as if you copy and paste the URL into the address bar of your browser (then also no referer is sent).
However, when you include it on your website on the Internet, your page URL is sent as referer. The "vclogos" website now appears to check this value and return nothing if a referer is sent which is not one of their own (I'd guess they accept only http://www.vclogos.co.uk/something so it will only work if it's included in their own website, not yours).
Long story short, you just can't load this image inside your website directly from this URL.
The only way would be to use a web request on the server side in ASP.NET which fetches the actual image and then serves it to the client as a resource which, from the browser's point of view, comes from your server (who got it from "vclogos" first, but without sending a referer). The thing is that if your server fetches the image data, you have control over what headers you send. (Unlike your website which, once loaded in the browser, doesn't have this control - the browser will send your page URL as referer and there is nothing you can do about that.)
Just a detail:
http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542
is a link to a blank page...
http://www.vclogos.co.uk/logo.php?subid=d18f655c3fc147&imgid=11542
is the link to an image.
You are sending an encoded URL. Always use the decoded URL (such as urlencode in php or HttpServerUtility.UrlEncode Method (String) in aspx)

Upload image to server with custom data

how can i upload an image (javascript) to a backend server (MVC4) and in the same post also add some custom data.
The problem we are facing is that we are trying to make an album web client much like facebook's albums.
A user will upload a series of photos to be added to a database. We need to have those photos already taged to an album as facebook's photos cannot be viewed without having an album who owns the photo.
I talked to the guy in charge of the front end and he says that while doing the javascript call to upload a photo, you cannot also send meta data within this call.
Thank you for your help.
Sure you can.
When you upload your file reference, just add some post variables to the URL.
like
upload.aspx?title=blah?date=somedate
the actual data is just part of the form data as specified by the HTTP protocol
http://www.vivtek.com/rfc1867.html
-------------------------------18788734234
Content-Disposition: form-data; name="nonfile_field"
value here
-------------------------------18788734234
Content-Disposition: form-data; name="myfile"; filename="ad.gif"
Content-Type: image/gif
[ooh -- file contents!]
-------------------------------18788734234--
A great tool is AjaxUpload it should be a good starting point. If you need to upload data along with it, add query variables to the "action"

Forcing image to download

I'm using a WebBrowser control in Silverlight and I'm setting it to a local HTML page. The HTML page has various links and they all work fine. Can I make it so that if the user clicks on an image file, it downloads to their system (or does the default behavior of the broweser) instead of displaying on the webpage? The main question is, is it possible to do this if I don't have access to the server itself? Thanks
edit - Is it possible to send an HTTPWebRequest to get the image and then edit the response headers, all from the client? This may be an alternative.
The standard way of doing this is to send the Content-Disposition HTTP header with attachment as the value. See here for more on this: Uses of content-disposition in an HTTP response header
But if you don't have access to the server, I don't think you can achieve this.

ASP.Net page or handler to load mp3/avi and send to browser - BOTH internal file path and external url as source

So there are many examples of getting the stream from a file locally, but suppose i want to stream an MP3 from another site.
I have a page that lists urls of MP3s, for example, one might be...
www.domain.com/getResource.aspx?ResourceId=123
This page can obviously fairly simply load Resource 123, and send to the browser.
Suppose resource 123 is not local, it is a URL to another site which serves up the mp3
so my question is what is the best way for this Resource Page to get the data from the remote source and send back to the browser. In effect, there should be no differential as far as the client is concerned where the file has come from. It always goes through this aspx (or ashx) page
Your site would need to make its own HTTP request to the remote server. Take the content of the response, and write it to the response stream of the request that came to your site.

Saving an image from HTTP Post (Not using FileUpload Control)

I have a page (May even use a handler), anyway.. that is awaiting an HTTP POST with a number of variables including an image encoded as a multipart/form-data Content Type.
How do I save the image from the HTTP Post?
This image encoded int the HTTP POST is from another site and is not from a local FileUpload Control.
EDIT :
Hang on, am I being daft.. can't I just use Request.Files..
May be as simple as http://dotnetslackers.com/articles/aspnet/Upload_multiple_files_using_the_HtmlInputFile_control.aspx
-- Lee
if the posted form is multipart/form-data you can just check the Request.Files collection to check if there should be any files posted with your form :)

Categories

Resources