数据库中存在文件的路径,如何下载数据库中指定路径的文件?

解决方案 »

  1.   

            private void downfile()
            {
                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("没有输入要下载的文件!");
                }
            } 自己完善一下
      

  2.   

    直接输出超链接,链接地址就是数据库中的地址
    <a href="文件路径">文件标题</a>
      

  3.   

    2楼的理解是对的,这个思路我也知道,但是我不会写代码啊
    例如:表 news 字段filestr中存在该文件的路径。我改如何取这个路径呢?这个超链接应该写在哪里?能写详细一点的代码吗?
      

  4.   

    using System;
    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);