Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a window application in vb.net. I need to upload some file to webserver (e.g. http:/www.domain.com/directory) form my window application (desktop).

I am new in vb.net. so please help me regarding my problem.

Thanks
Posted

If that website is yours then can have a interface with file upload. Access the webpage using WebBrowser control in your windows application. Upload the file.

Or else your domain.com has ftp service then you can use the FtpWebRequest class. Here is an example.

http://www.vcskicks.com/csharp_ftp_upload.php[^]
 
Share this answer
 
Comments
Eduard Keilholz 18-Mar-11 8:05am    
Agree
kals84 18-Mar-11 8:09am    
ok. fine i can work with your method.. what if i want to show progress bar during file upload.
Albin Abel 18-Mar-11 8:23am    
if you want to show progress bar then write to the stream in chunks. Say each chunk is 10kb then calculate percentage of it from the file size then the progress bar value would be ((no of chunks written* chunk size)/file size)*100. As you know the Stream class has over loaded method set the start positions and length of the bytes to write reqStream.Write(buffer, 0, buffer.Length);. So any problem?
You need to use the HttpWebRequest object. I found this code (google is free), but it needs some try/catch stuff put in, and it wouldn't be a bad idea to optimize it a little. You shouldn't have any trouble converting it to VB with one of the many free converters available on the web.

C#
public static  void UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc) 
{      
    long length = 0;     
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");       
    HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
    httpWebRequest2.Method      = "POST";
    httpWebRequest2.KeepAlive   = true; 
    httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;    

    Stream memStream = new System.IO.MemoryStream();  
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");   
    string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
    foreach (string key in nvc.Keys) 
    {    
        string formitem = string.Format(formdataTemplate, key, nvc[key]); 
        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); 
        memStream.Write(formitembytes, 0, formitembytes.Length); 
    } 
    memStream.Write(boundarybytes, 0, boundarybytes.Length);  
    string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";  
    for (int i = 0; i < files.Length; i++) 
    { 
        //string header = string.Format(headerTemplate, "file" + i, files[i]);  
        string header = string.Format(headerTemplate, "uplTheFile", files[i]);   
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);  
        memStream.Write(headerbytes, 0, headerbytes.Length);  
        FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);  
        byte[] buffer = new byte[1024]; 
        int bytesRead = 0;  
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)  
        {   
            memStream.Write(buffer, 0, bytesRead);
        } 
        memStream.Write(boundarybytes, 0, boundarybytes.Length); 
        fileStream.Close(); 
    } 
    httpWebRequest2.ContentLength = memStream.Length;  
    Stream requestStream = httpWebRequest2.GetRequestStream();  
    memStream.Position = 0;     
    byte[] tempBuffer = new byte[memStream.Length]; 
    memStream.Read(tempBuffer, 0, tempBuffer.Length); 
    memStream.Close(); 
    requestStream.Write(tempBuffer, 0, tempBuffer.Length); 
    requestStream.Close();   
    WebResponse webResponse2 = httpWebRequest2.GetResponse();  
    Stream stream2 = webResponse2.GetResponseStream(); 
    StreamReader reader2 = new StreamReader(stream2);   
    MessageBox.Show(reader2.ReadToEnd());  
    webResponse2.Close(); 
    httpWebRequest2 = null; 
    webResponse2 = null; 
}
 
Share this answer
 
Comments
Albin Abel 18-Mar-11 8:12am    
:) my 5
Sergey Alexandrovich Kryukov 20-Mar-11 22:55pm    
This one is good, my 5.
--SA
There are a couple of ways to do that.

You can either choose the install a FTP server on the webserver and choose to implement an FTP Client[^] in your software.

You can also add a webservice to your website which accepts file upload.

I recommend using the FTP option if you're able to install a FTP Server since FTP is way better in sending files from one location to another then a webservice is.

Good luck!
Eduard
 
Share this answer
 
Comments
Albin Abel 18-Mar-11 8:10am    
good link. My 5
Use POST with http classes to post the file to a web page which will then
save it to the required location.

this[^] article will guide you. But it is in C#
 
Share this answer
 
v2
Comments
Albin Abel 18-Mar-11 8:09am    
Ramalinga OP asking about windows application. However the microsoft link is good. my 5
[no name] 18-Mar-11 8:32am    
Thanks AlbinAbel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900