C # public class for HTTP File Download (supports resumable upload)

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. IO;
Using System. net;

Namespace Xcdownloadfile
{
Public Class Downloadfile
{
///
/// File Download Method
///
/// File Storage path and file name
/// Returns the Server File Name.
///
Public Bool Deownloadfile ( String Strfilename, String File)
{
Bool Flag = False ;
// Open the last downloaded file
Long Sposition = 0 ;
// Instantiate Stream Object
Filestream fstream;
// Determine whether the folder to be downloaded exists
If (File. exists (strfilename ))
{
// Open the file to be downloaded
Fstream = file. openwrite (strfilename );
// Get the downloaded Length
Sposition = fstream. length;
Fstream. Seek (sposition, seekorigin. Current );
}
Else
{
// Create a file without saving the file
Fstream = New Filestream (strfilename, filemode. Create );
Sposition = 0 ;
}
Try
{
// Open Network Connection
Httpwebrequest myrequest = (httpwebrequest) httpwebrequest. Create ( " Http://www.newxing.com/download/ " + File );
If (Sposition> 0 )
Myrequest. addrange (( Int ) Sposition ); // Set range value
// Request from the server to obtain the server's response data stream
Stream mystream = myrequest. getresponse (). getresponsestream ();
// Define a byte of data
Byte [] Btcontent = New Byte [ 512 ];
Int Intsize = 0 ;
Intsize = mystream. Read (btcontent, 0 , 512 );
While (Intsize> 0 )
{
Fstream. Write (btcontent, 0 , Intsize );
Intsize = mystream. Read (btcontent, 0 , 512 );
}
// Close stream
Fstream. Close ();
Mystream. Close ();
Flag = True ; // Returns true. Download succeeded.
}
Catch (Exception)
{
Fstream. Close ();
Flag = False ; // Returns false. Download failed.
}
Return Flag;
}
}
}
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.