PDA

View Full Version : سوال: عدم دانلود کامل فایل



r_khan
پنج شنبه 23 شهریور 1391, 10:20 صبح
من برای دانلود از کد زیر استفاده میکنم و فایلم 3 مگ به بالاست ولی وقتی دانلود میکنم حجم فایل 12.3 کیلو بایت است:گیج:



string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "";
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + name);
}
if (type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();

r_khan
پنج شنبه 23 شهریور 1391, 14:44 عصر
ناگفته نماند فایل من بکاپ دیتابیس است با پسوند .bak ایا این موضوع تاثیر داره:متفکر: اخه فایلهای دیگر را بدون مشکل انجام میده:گیج:

E_Zabihi
پنج شنبه 23 شهریور 1391, 18:05 عصر
سلام
اولاً بهتره از بافر استفاده کنی
ثانیاً توی web.config و نود httpRuntime برای executionTimeout و maxRequestLength مقادیر بیشتری وارد کنی تا تایموآوت نشه.

یه کد نمونه دانلود هم اینه :


System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = "DownloadFileName";

// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read );


// Total bytes to read:
dataToRead = iStream.Length;

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer= new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.Close();
}