数据库中存在文件的路径,如何下载数据库中指定路径的文件?
解决方案 »
- 如何让一个类能够同时被DataContractJsonSerializer序列化成JSON,又能够通过IFormatter序列化到文件
- 重新提问 GDI+动态绘图的问题
- 无法将文件“obj\Debug\PMS.exe”复制到“bin\Debug\PMS.exe”。
- 在SelectedIndexChanged后,让GridView1返回到Itemtmeplate页面
- 根據一url地址,自動取得此地址之內容,並將此內容以mail內容發送?
- 问一个弱弱的问题,急,在线等
- 大家看这段代码,关于OnItemDataBound方法,一点都不明白啊
- 求精通VC和C#高手
- 使用反射,如何得到或使用重载函数
- 关于C#中API函数的调用问题,请高手指教?
- .net图片上传截图时提示内存不足,求解答
- ASP.NET web site???
{
string FileName;
WebClient DownFile = new WebClient();
long fbytes;
if (textBox1.Text != "")
{
saveFileDialog1.ShowDialog();
FileName = saveFileDialog1.FileName;
if (FileName != "")
{
//取得文件大小
WebRequest wr_request = WebRequest.Create(textBox1.Text);
WebResponse wr_response = wr_request.GetResponse();
fbytes = wr_response.ContentLength;
progressBar1.Maximum = (int)fbytes;
progressBar1.Step = 1;
wr_response.Close();
//开始下载数据
DownFile.DownloadData(textBox1.Text);
Stream strm = DownFile.OpenRead(textBox1.Text);
StreamReader reader = new StreamReader(strm);
byte[] mbyte = new byte[fbytes];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
while (fbytes > 0)
{
int m = strm.Read(mbyte, startmbyte, allmybyte);
if (m == 0) break;
startmbyte += m;
allmybyte -= m;
progressBar1.value += m;
}
FileStream fstrm = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
fstrm.Write(mbyte, 0, startmbyte);
strm.Close();
fstrm.Close();
progressBar1.value = progressBar1.Maximum;
}
}
else
{
MessageBox.Show("没有输入要下载的文件!");
}
} 自己完善一下
<a href="文件路径">文件标题</a>
例如:表 news 字段filestr中存在该文件的路径。我改如何取这个路径呢?这个超链接应该写在哪里?能写详细一点的代码吗?
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading;public class Download
{
public static 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; int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor((decimal)(1000 * pack / _speed)) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
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);
int maxCount = (int)Math.Floor((decimal)((fileLength - startBytes) / pack)) + 1; for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
}Download.ResponseFile(Page.Request, Page.Response, System.IO.Path.GetFileName(e.CommandArgument.ToString()), Server.MapPath(e.CommandArgument.ToString()), 10240000);