ASP. NET upload and download file example, implemented using WebClient

Source: Internet
Author: User
/// <Summary>
/// WebClient uploads a file to the server
/// </Summary>
/// <Param name = "filenamepath"> file name, full path format </param>
/// <Param name = "uristring"> server folder path </param>
Public String uploadfile (string filenamepath, string uristring)
{
String filename = filenamepath. substring (filenamepath. lastindexof ("\") + 1 );
String newfilename = datetime. Now. tostring ("yymmddhhmmss") + datetime. Now. millisecond. tostring () + filenamepath. substring (filenamepath. lastindexof ("."));
String filenameext = filename. substring (filename. lastindexof (".") + 1 );
If (uristring. endswith ("/") = false)
{
Uristring = uristring + "/";
}
Uristring = uristring + newfilename;
// Create a WebClient instance
WebClient mywebclient = new WebClient ();
Mywebclient. Credentials = credentialcache. defaultcredentials;
// File to be uploaded
Filestream FS = new filestream (filenamepath, filemode. Open, fileaccess. Read );
// Filestream FS = openfile ();
Binaryreader r = new binaryreader (FS );
Try
{
// The uploadfile method can be used in the following format
// Mywebclient. uploadfile (uristring, "put", filenamepath );
Byte [] postarray = R. readbytes (INT) fs. Length );
Stream poststream = mywebclient. openwrite (uristring, "put ");
If (poststream. canwrite)
{
Poststream. Write (postarray, 0, postarray. Length );
}
Else
{
Return "no write .";
}
Poststream. Close ();
Return "succefull ";
}
Catch (exception ex)
{
Return "error:" + ex. message;
}
}
/// <Summary>
/// Download the server file to the client
/// </Summary>
/// <Param name = "url"> address of the downloaded file, absolute path </param>
/// <Param name = "dir"> another directory </param>
Public String download (string URL, string DIR)
{
WebClient client = new WebClient ();
String filename = URL. substring (URL. lastindexof ("/") + 1); // The downloaded file name
String Path = dir + filename; // save as absolute path + file name
Try
{
Webrequest myre = webrequest. Create (URL );
}
Catch (exception ex)
{
Return "error:" + ex. message;
}
Try
{
Client. downloadfile (URL, PATH );
Return "succefull ";
}
Catch (exception ex)
{
Return "error:" + ex. message;
}
}

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.