C# How to upload file by http? - c#

Im looking for solution how can I upload some file using http request. I got the idea that I'll transfer my files by post and on PHP side I'll put files on the server.
How doin this?

var client = new System.Net.WebClient();
client.UploadFile(address, filename);
See UploadFile on MSDN.

Well I'd try the simplest possible thing that might work to start with - WebClient.UploadFile:
WebClient client = new WebClient();
client.UploadFile(url, file);
Of course, you'll have to write appropriate PHP code to handle the upload...

One more approach is to upload file via browser and handle upload request/response with Fiddler. After that, you can write exact request using HttpWebRequest via C#.

Related

Accessing downloaded file instead of page HTML

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.

FileUpload in C# with PHP Server

I am currently working on a fileUploader in the C# language for a image hosting site I develop for, I am currently wondering what the best approach is to uploading files to the server(all php/sql) with the client software being C#.
Also, how would I process these uploads in a PHP script, would you say something along the lines of:
upload.php?appfile=filepath?
Thanks,
You can use the WebClient Class and its UploadFile Method:
using (var wc = new WebClient())
{
wc.UploadFile("http://www.example.com/upload.php", "somefile.dat");
}
The UploadFile Method uses the POST method to upload the file to the HTTP resource; you need to set up your PHP script accordingly.

C# Uploading files via HTTP

is it possible to upload files to web page and then access file data on website using $_FILES['file'] in php?
Use the following code to send file name to php file as POST value and it also stores the returned value
WebClient client = new WebClient();
byte[] bret = client.UploadFile(webpath, "POST", filepath);
string sret = System.Text.Encoding.ASCII.GetString(bret);
You have to modify your webserver in some way to answer POST requests with the appropriate actions. If you can't acces your server, you can't upload to it with C#; there is no 'standard' way to do this.

Upload file to server in Windows mobile C# project

We have a setup of server and windows mobile device as a client. In server CSI script ready to accept single file from client.
In Desktop we have use WebClient.UploadFile method to upload file to server, but in windows mobile this isn't implemented, till now we haven't found any alternative method to achieve same.
Thanks in advance.
Ramanand
When using the .NET Compact Framework, you can use System.Net.HttpWebRequest instead of WebClient, which isn't supported on .NET CF.
Since WebClient is implemented on top of HttpWebRequest, you can do everything with HttpWebRequest that you can with WebClient, albeit with more code.
For example, to download the contents of a URL into a string, you can use this code:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
string html;
using (var r = request.GetResponse().GetResponseStream())
{
using(var r2 = (TextReader)new StreamReader(r))
{
html = r2.ReadToEnd();
}
}
You should be able to use the method in this post, you could maybe do some refactoring to fit your purpose better.
Upload files with HTTPWebrequest (multipart/form-data)

How do I upload an image/file using a post request in C#?

C# provides functionality to submit a post request, but there is nothing about uploading an image/file on MSDN. I'd like to do this without using raw headers.
Related questions
Upload files with HTTPWebrequest (multipart/form-data)
You can use WebClient class easily. It has an UploadFile method:
var client = new WebClient();
client.UploadFile("http://server/upload.aspx", #"C:\file.jpg");
My ASP.NET Upload FAQ has an article on this, with example code: Upload files using an RFC 1867 POST request with HttpWebRequest/WebClient. This code doesn't load files into memory, supports multiple files, and supports form values, setting credentials and cookies, etc.

Categories