日期:2014-05-18  浏览次数:20926 次

求下载文件的网页代码???
就是点击图标或文字,就会出现文件另存为的窗口,什么样的代码可以实现???急求,谢谢——

文件是从数据库中读出来的,点击下载的时候就会保存相应的文件,要怎么写代码?

------解决方案--------------------
public bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try {
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try {
_Response.AddHeader( "Accept-Ranges ", "bytes ");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
Int16 pack = 10240;
Int16 sleep = Convert.ToInt16(Math.Floor(1000 * pack / _speed) + 1);
if (!(_Request.Headers( "Range ") == null)) {
_Response.StatusCode = 206;
char[] splitChar = new char[1];
splitChar(0) = "= ";
splitChar(1) = "- ";
string[][0] range = _Request.Headers( "Range ").Split( "a ");
startBytes = Convert.ToInt64(range(1));
}
_Response.AddHeader( "Content-Length ", (fileLength - startBytes).ToString());
if ((startBytes != 0)) {
_Response.AddHeader( "Content-Range ", string.Format( " bytes {0}-{1}/{2} ", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader( "Connection ", "Keep-Alive ");
_Response.ContentType = "application/octet-stream ";
_Response.AddHeader( "Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
long maxCount = Convert.ToInt64(Math.Floor((fileLength - startBytes) / pack) + 1);
long i;
for (int i = 0; i <= maxCount; i++) {
if ((_Response.IsClientConnected)) {
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
} else {
i = maxCount;
}
}
} catch (Exception e) {
return false;
} finally {
br.Close();
myFile.Close();
}
} catch {
return false;
}
}
------解决方案--------------------
在代码中使用FileSaveDialog,再使用FileStream ,上面那位FileStream 如何使用已经说得很详细了