C # file upload/download

Source: Internet
Author: User

1. File Upload

/// <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>
Private void uploadfile (string filenamepath, string uristring)
{
String filename = filenamepath. substring (filenamepath. lastindexof ("\") + 1 );
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;
Upfilename = 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
{
MessageBox. Show ("file cannot be written currently! ");
}
Poststream. Close ();
}
Catch
{
MessageBox. Show ("File Upload Failed. Please try again later ~ ");
}
}

 

2. File Download

String filepath = system. appdomain. currentdomain. basedirectory. tostring () + "programout. xml"; // path
WebClient client = new WebClient ();
String filename = filepath. substring (filepath. lastindexof ("\") + 1); // The downloaded file name
Savefiledialog = new savefiledialog ();
Savefiledialog. Filter = "XML file | *. xml ";
Savefiledialog. filterindex = 2;
Savefiledialog. restoredirectory = true;
If (savefiledialog. showdialog () = dialogresult. OK)
{
String fname = savefiledialog. filename;
String Path = fname; // save as absolute path + file name
Try
{
Webrequest myre = webrequest. Create (PATH );
}
Catch
{
MessageBox. Show ("File Download failed", "error ");
}
Try
{
Client. downloadfile (filepath, PATH );
MessageBox. Show ("file exported successfully !! "," System prompt! ", Messageboxbuttons. OK, messageboxicon. information );
}
Catch
{
MessageBox. Show ("File Download failed", "error ");
}

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.