|
是这样的,我写了一段下面的代码,下服务器上一个文本文件,现在可以这个文本文件是的确存在的,而且此代码在本地,我其它2台web服务器(win2k3)都ok,只有在其中一台web服务器下载时,文本文件找开为空--没有内容,请大家帮忙!
private void DownloadTemplate(string filepath)
{
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
string filename = System.IO.Path.GetFileName(filepath);
try
{
if (!System.IO.File.Exists(filepath))
return;
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=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)); // 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
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
showError("", "StyleDef.ascx", "DownloadTemplate", "", "Fail: doing download for stylefile. message:" + ex.Message, "");
}
finally
{
if (iStream != null)
{ //Close the file.
iStream.Close();
}
Response.Close();
}
} |
|