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.
Related
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#.
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.
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)
I'm using .Net.
Is it possible for a web form to upload an image from a web or FTP server? The files I need to submit are on the web. If this is possible, code snippet is appreciated.
Yes, it is possible.
You can use the WebClient class to interact with other web servers in .Net server-side code.
For example:
using(var client = new WebClient())
client.UploadFile("ftp://server/path", #"C:\path\to\file");
If the file is on a different website, you can write the following:
using(var client = new WebClient())
client.UploadData("ftp://server/path", client.DownloadData("http://server/path"));
You can read and write FTP, HTTP, and HTTPS urls interchangeably.
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.