我的数据表是这样的
ID File
1 a.doc
2 ab.doc
3 x.doc
......文件都是存在d:\wen\下的,请问大家怎么能显示成这样
序列号 文件
1 a.doc
2 ab.doc
3 x.doc用户点击a.doc等文件名后就可以打开或者另存为此doc文件
ID File
1 a.doc
2 ab.doc
3 x.doc
......文件都是存在d:\wen\下的,请问大家怎么能显示成这样
序列号 文件
1 a.doc
2 ab.doc
3 x.doc用户点击a.doc等文件名后就可以打开或者另存为此doc文件
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition","attachment; filename=abc.doc");//以附件形式打開 也就是彈出對話框
Response.AddHeader("Content-Disposition","inline; filename=abc.doc");//在線打開
Response.OutputStream.Write(b , 0, b.Length);
Response.Flush();
string fileName = Request.QueryString["fileName"].ToString();
string filePath = Server.MapPath("~/Upload/UploadFile/") + fileName;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
string[] fileNameSplit = fileName.Split('_');
string myFileName=null;
for (int i = 1; i < fileNameSplit.Length; i++)
{
myFileName +=fileNameSplit[i];
}
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
try
{
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(myFileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
catch (Exception ex)
{
string error = ex.ToString();
}
finally
{
Response.Close();
}
}写一个方法需要参数为fileName,放在一个页面(download)的PageLoad里,将XXX.DOC做成一个超链接,利用URL传值给(download)传去fileName即可