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;
}
}