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