Use WebClient to upload files in winform

Source: Internet
Author: User

Upload the specified local file to the specified resource using the specified Method

Upload files to the server through HTTP in winform

1. server-side ProcessingProgram

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Upload. aspx. CS " Inherits = " Upload "   %>

Front-endCodeUpload. aspx

Using System;
Using System. Web;

Public Partial Class Upload: system. Web. UI. Page
{
// Default storage path of the server
Private   Readonly   String Serverpath =   @" C: \ upload \ " ;

Private   Void Page_load ( Object Sender, system. eventargs E)
{ // Obtain the file submitted by HTTP and rename it and save it
Foreach ( String Key In Request. Files. allkeys)
{
Httppostedfile File = Request. Files [Key];
String Newfilename = Datetime. Now. tostring ( " Yymmddhhmmssffff " )
+ File. filename. substring (file. filename. lastindexof ( ' . ' ));

Try
{ // Save the file and return the relative path address.
File. saveas ( This . Serverpath + Newfilename );
Response. Write ( " Upload/ "   + Newfilename );
}
Catch (Exception)
{
}
}
}
}

Background code upload. aspx. CS
 

2. Client Program

/**/ ///   <Summary>
/// Upload a single file to the server
///   </Summary>
///   <Param name = "uriaddress"> Uri of the received file resource, for example: Http: // xxxx/upload. aspx </Param>
///   <Param name = "filepath"> Resource file to be sent, for example, @ "D: \ workspace \ WebService related .doc </Param>
///   <Returns> Returns the relative path of the file to be saved, for example, "upload/xxxxx.jpg" or an error is returned "" </Returns>
Private   String Uploadfile ( String Uriaddress, String Filepath)
{
// Use WebClient
System. net. WebClient =   New System. net. WebClient ();
WebClient. Credentials = System. net. credentialcache. defaultcredentials;
Try
{
Byte [] Responsearray = WebClient. uploadfile (uriaddress, " Post " , Filepath );
String Savepath = System. Text. encoding. ASCII. getstring (responsearray );
Return Savepath;
}
Catch (Exception)
{
Return "";
}
}

PS. Determine whether the remote file exists

Public   Bool Uriexists ( String URL)
{
Try
{
NewSystem. net. WebClient (). openread (URL );
Return True;
}
Catch (System. net. webexception)
{
Return False;
}
}  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.