Good afternoon,
I need to make a windows application that upload files (jpg) that are on my computer to a web hosting.
Have tried various codes that I saw on the net but none worked.
Does anyone have a code that works that I intend to do this? May be in C# or VB.NET
Paulo
Suthish NairPosted Jan 19, 2011, 5:55 AM
First of all you need rights to upload files to the url using.
Check with your team necessary ports are opened or not.
Guest UserPosted Jan 18, 2011, 10:57 AM
I can you give two different methods to upload files from a desktop application to a webserver:
By using FTP (this is the easier one):
public void UploadMyFile() {
// Get the object used to communicate with the server.
string myFtp = ftp://mydomain.com/fileOnFtp.html;
string myFtpUserName = "aassddff";
string myFtpPassword = "xxxxxxx";
string myLocalFile = @"c:\\text.txt";
System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(myFtp);
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
request.Credentials = new System.Net.NetworkCredential(myFtpUserName, myFtpPassword);
//get your local file into stream
System.IO.StreamReader sr = new System.IO.StreamReader(myLocalFile);
byte[] fileInBytes = System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd());
sr.Close();
request.ContentLength = fileInBytes.Length;
System.IO.Stream reqStream = request.GetRequestStream();
reqStream.Write(fileInBytes, 0, fileInBytes.Length);
reqStream.Close();
System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
Console.WriteLine("File has been successfully uploaded. " + response.StatusDescription);
response.Close();
}
. By using a Web Service:
This is a harder one, and requires more coding. So, I'll just tell you the steps
public string UploadFile (string fileName, string fileContent)
fileName: name of the file to be uploaded.
fileContent: file's content in Base64 string
The method should convert Base64 to byte array and saves it on the web server.
Read the file from the local drive. Conver the file's content to Base64 string. Then call the UploadFile web method.
As I mentioned the second one requires more coding, but I think it is a better solution; you can check your file's content, add some extra security.
good luck