Skip to content Skip to sidebar Skip to footer

Uploading File Using Httpwebrequest

I want to upload file to a server. I wrote this function to upload the file to localhost server (I am using wamp server): private void button1_Click_1(object sender, EventArgs e)

Solution 1:

try with webClient

WebClientclient=newWebClient();
 byte[] bret = client.UploadFile(path, "POST", FilePath);
//path==URL//FilePath==Your uploading file path

or

WebClientwebClient=newWebClient(); 
stringwebAddress=null; 
try 
{ 
    webAddress = @"http://localhost/upload_file/"; 

    webClient.Credentials = CredentialCache.DefaultCredentials; 

    WebRequestserverRequest= WebRequest.Create(webAddress); 
    serverRequest.Credentials = CredentialCache.DefaultCredentials;
    WebResponse serverResponse; 
    serverResponse = serverRequest.GetResponse(); 
    serverResponse.Close(); 

    webClient.UploadFile(path, "POST", FilePath); 
    webClient.Dispose(); 
    webClient = null; 
} 
catch (Exception error) 
{ 
    MessageBox.Show(error.Message); 
} 

(code in or part i didn't tried )

Post a Comment for "Uploading File Using Httpwebrequest"