Click here to Skip to main content
15,888,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help suppose i have windows form contain open dialog and button ineed to

uploade file to webserver (http://localhost:79/uploade/) how i can do this


help me

please
Posted
Updated 24-Oct-16 3:03am
v3
Comments
Sergey Alexandrovich Kryukov 1-Feb-12 6:33am    
Please remove "Apache" from tags, add "WinForms". You should be the most interested in those tags, it affects your chances to get a useful advice (in this case, you already got it :-)

Don't confuse people -- you already got one totally irrelevant would-be-the-answer.
--SA

You need to use the class System.Net.HttpWebRequest with the HTTP request method "POST".

Please see:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^],
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

You can find the upload sample here: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data[^].

—SA
 
Share this answer
 
v2
Comments
Sridhar Patnayak 1-Feb-12 6:47am    
Quickest way to implement - Upload sample from stackoverflow - 5+
Sergey Alexandrovich Kryukov 1-Feb-12 6:48am    
I would thoroughly examine the code, but yes, basically.
Thank you, Sridhar.
--SA
NeptuneHACK! 1-Feb-12 8:19am    
My 5 as usual ^_^
Sergey Alexandrovich Kryukov 10-Jul-13 1:56am    
Thank you!
—SA
odai_dahmos 1-Feb-12 15:43pm    
thank you
You can up-load the file by using UploadFile Method of WebClient.
Please refer to the MSDN library for details.

WebClient Class
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx[^]

WebClient.UploadFile Method
http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(VS.80).aspx[^]

WebClient.UploadFile Method (String, String)
http://msdn.microsoft.com/en-us/library/36s52zhs(VS.80).aspx[^]

Thanks
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-12 6:35am    
Yes, I provided the solution, please see, but this is another way which should work, my 5.
--SA
odai_dahmos 1-Feb-12 15:43pm    
thank you
in windows form:
C#
private void uploadButton_Click(object sender, EventArgs e)
{
    var openFileDialog = new OpenFileDialog();
    var dialogResult = openFileDialog.ShowDialog();    
    if (dialogResult != DialogResult.OK) return;              
    Upload(openFileDialog.FileName);
}

private void Upload(string fileName)
{
    var client = new WebClient();
    var uri = new Uri("http://www.yoursite.com/Uploader/");
    {
        client.Headers.Add("fileName", System.IO.Path.GetFileName(fileName));
        client.UploadFileAsync(uri, fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

in server:
C#
[HttpPost]
public async Task<object> Uploader()
{
    var file = await Request.Content.ReadAsByteArrayAsync();
    var fileName =Request.Headers.GetValues("fileName").FirstOrDefault();
    var filePath = "/upload/files/";
    try
    {
        File.WriteAllBytes(HttpContext.Current.Server.MapPath(filePath) + fileName, file);           
    }
    catch (Exception ex)
    {
        // ignored
    }

    return null;
}
 
Share this answer
 
v5
Comments
mukesh889 7-Dec-17 6:10am    
code is uncompleted

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