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.
Related
I am currently working on a simple app to make my life easier. I made an android app which lets me pick a file and uploads it to a server. I am working on a windows PC c# app that sends its ip (dynamic) and its open port to my server.
Whenever the server receives a file from my phone, I want it to send a POST request to my PC.
I am fairly new to web stuff (I have done tons of coding before though), but as far as I understand only a server can receive a POST request.
How can I make a C# server that runs on my PC with a dynamic IP and receives POST requests?
I have been struggling with this for a while now, just simple keywords I should research would be very helpful, thanks.
HTTP is a protocol which lets Web Servers and Clients communicate. It requires you to have a web server (IIS, Apache or other) to respond to client http requests.
Client can send a GET, POST and others request type messages.
The prefable way is to send a web client using a WebClient class. Here is a sample taken from another answer given by Andrew
string URI = "site.com/mail.php";
using (WebClient client = new WebClient())
{
System.Collections.Specialized.NameValueCollection postData =
new System.Collections.Specialized.NameValueCollection()
{
{ "to", emailTo },
{ "subject", currentSubject },
{ "body", currentBody }
};
string pagesource = Encoding.UTF8.GetString(client.UploadValues(URI, postData));
}
I would create some kind of server application using WebAPI, SignalIR, WCF or ASMX web services. All of these can handle server/client communication and would make it easy to communicate with your device.
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#.
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.
I have a C#.net webform that does a simple response.write in content type JSON format. It works greats from every client I test it with - including a TinyWebDB API call from an Android phone.. but don't worry about that API for this question.
I added some serverside code to that web form to read and scrape a web page as follows.
System.Net.WebClient myWebClient = new System.Net.WebClient();
Stream myStream = myWebClient.OpenRead(what);
StreamReader sr = new StreamReader(myStream);
string s = sr.ReadToEnd();
I'm under the impression that code is all Server Side!?
I tested the page from IE, FF and Chrome.. all work great. However If I attempt to call the web form page from TinyWebDB the call works great and I get data back, but I get a 404 error on the server side read of the webpage??
It's almost as if System.Net.WebClient requires something from or is doing something on client itself. i thought the reading of the page was all happening serverside and behind the scenes on my serer. Why would my serverside code care about what browser or API initiated the call to the webform?
Should I be using another class?
Many Thanks.
That is serverside code.
Where does the variable "what" get set? Is the form TinyWebDB client munging your form input values?
Is it possible that the site you are requesting is rejecting your request because you're hitting it too fast and you've just been unlucky with it being that client every time?
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)