Asp.net c # Use FtpWebRequest to upload and download an object instance

Source: Internet
Author: User

Asp tutorial. net c # Use FtpWebRequest to upload and download an object instance
Create an FtpWebRequest object based on uri
FtpWebRequest ftpReq = (FtpWebRequest) FtpWebRequest. Create (new Uri (strUri ));
// Ftp user name and password
FtpReq. Credentials = new NetworkCredential ("username", "password ");
// Specify the command to be executed
FtpReq. Method = WebRequestMethods. Ftp. UploadFile;
// The connection is closed after a command is successfully executed.
FtpReq. KeepAlive = false;
// Specify the data transmission type as binary
FtpReq. UseBinary = true;
// Notify the server of the file size when uploading the file
FtpReq. ContentLength = fi. Length;
// Set the buffer size to 2 kb.
Int buffLength = 2048;
Byte [] buff = new byte [buffLength];

Int contentLen;

Try
{
// Open a file stream to read the uploaded file
FileStream fs = fi. OpenRead ();
// Write the uploaded file to the stream
Stream strm = ftpReq. GetRequestStream ();

// 2kb of each file stream read
 

ContentLen = fs. Read (buff, 0, buffLength );

While (contentLen! = 0) // The stream content has not ended
{
// Write content from file stream to upload stream
Strm. Write (buff, 0, contentLen );
ContentLen = fs. Read (buff, 0, buffLength );
}
Strm. Close ();
Fs. Close ();
}
Catch (Exception e)
{

}


'Method 2
 

FileInfo file = new FileInfo ("G:/www. bKjia. c0m/interior design 01.jpg ");
If (file. Exists)
{
Response. Clear ();
Response. AddHeader ("Content-Disposition", "attachment; filename =" + Server. UrlEncode (file. Name ));
Response. AddHeader ("Content-Length", file. Length. ToString ());
Response. ContentType = "application/octet-stream ";
Response. Filter. Close ();
Response. WriteFile (file. FullName );
Response. End ();
}


 

Related Article

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.