I created a script in ASP.NET which list the files from specified folder.
protected void Page_Load( object sender, EventArgs e ) {
//get file list
DirectoryInfo di = new DirectoryInfo( Server.MapPath( "~/download" ) );
int i = 0;
foreach ( FileInfo fi in di.GetFiles() ) {
HyperLink link = new HyperLink();
link.ID = "HyperLink" + ( i++ );
link.ToolTip = fi.Length.ToString();
link.Text = fi.Name;
link.NavigateUrl = "downloading.aspx?file=" + fi.Name;
Page.Controls.Add( link );
Page.Controls.Add( new LiteralControl( "<br/>" ) );
}
}
In downloading.aspx
file, I have :
protected void Page_Load( object sender, EventArgs e ) {
string filename = Request["file"].ToString();
FileDownload( filename, Server.MapPath( "~/download/" + filename ) );
}
/// <summary>
/// Download specified file and his url
/// </summary>
/// <param name="filename"></param>
/// <param name="fileUrl"></param>
private void FileDownload( string filename, string fileUrl ) {
Page.Response.Buffer = true;
Page.Response.Clear();
DownloadFile df = new DownloadFile();
bool success = df.DownloadIt( Page.Response, filename, fileUrl );
if ( !success ) {
Response.Write( "Download error" );
}
Page.Response.End();
}
And the DownloadIt
method is defined:
public bool DownloadIt( HttpResponse response, string filename, string fileUrl ) {
if ( response == null ) {
throw new Exception();
}
if ( string.IsNullOrEmpty(filename) ) {
throw new ArgumentNullException( "filename" );
}
try {
response.ContentType = "application/octet-stream";
response.AppendHeader( "Content-Disposition", "attachment; filename=" + filename );
response.TransmitFile( fileUrl );
} catch {
return false;
}
return true;
}
In that list in default page, I have a file which has 5-6 MB and when I clicked on it, the dialog browser appears (that with 'Save' 'Open' 'Cancel' buttons) but there is a field where the file size is 14 bytes....
Why ?
Other small files, the size appears correctly.
Have I to do with Stream buffer ?
I solved problem with the following code:
try {
FileInfo fileDownload = new FileInfo( fileUrl );
int bufferSize = 0;
bufferSize = (int)fileDownload.Length;
using ( Stream s = new FileStream( fileUrl, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize) ) {
byte[] buffer = new byte[bufferSize];
int count = 0;
int offset = 0;
response.ContentType = "application/octet-stream";
response.AddHeader( "Content-Length", buffer.Length.ToString() );
response.AddHeader( "Content-Disposition", "attachment; filename=" + filename );
while ( ( count = s.Read( buffer, offset, buffer.Length ) ) > 0 ) {
response.OutputStream.Write( buffer, offset, count );
}
bufferSize = 0;
}
} catch {
return false;
}
Now the problem becomes for files which have > 200 MB due of bufferSize
which is int and it is a right parameter for FileStream
.
Try,
byte []bytes=System.IO.File.ReadAllBytes(fileUrl);
response.Clear();
response.ClearHeaders();
response.AddHeader("Content-Type","application/octet-stream");
response.AddHeader("Content-Length",bytes.Length.ToString());
response.AddHeader( "Content-Disposition", "attachment; filename=" + filename );
response.BinaryWrite(bytes);
response.Flush();
response.End();
You can manually set the Content-Length
header. See this question for details: Download .xlsx file using Response.TransmitFile()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.