当前位置:编程学习 > C#/ASP.NET >>

关于下载功能实现,请人指导一下?

我在VS2008里做了基于WEB的程序,要用到下载功能,我已经上传上去了。下载用的是LinkButton控件里的属性,NavigateURL,但是下载出错,请问应该正确的下载应该怎么样实现? --------------------编程问答-------------------- 请人指导。。。 --------------------编程问答-------------------- (1)不知道你怎么实现的
(2)不知道你怎么出错的

只能给你个参考:
http://www.google.com.hk/search?hl=zh-CN&source=hp&biw=1188&bih=585&q=asp.net%E5%AE%9E%E7%8E%B0%E4%B8%8B%E8%BD%BD%E5%8A%9F%E8%83%BD&oq=asp.net+%E5%AE%9E%E7%8E%B0%E4%B8%8B%E8%BD%BD&aq=0g&aqi=g-g2&aql=&gs_sm=e&gs_upl=453l5344l0l15l13l0l0l0l0l469l1531l2.3.2.0.1 --------------------编程问答--------------------
#region 文件下载
       public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
       {
           
           bool result = true;
           
           
           string tmpURL = hostURL;
          
           byteCount = byteCount * 1024;
           hostURL = tmpURL + "&npos=" + cruuent.ToString();
           
           System.IO.FileStream fs;  
           fs = new FileStream(localPath, FileMode.OpenOrCreate);
           if (cruuent > 0)
           {
               //偏移指针
               fs.Seek(cruuent, System.IO.SeekOrigin.Current); 
           }


           System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
           if (cruuent > 0)
           {
               request.AddRange(Convert.ToInt32(cruuent));    //设置Range值
           }

           try
           {
               //向服务器请求,获得服务器回应数据流
               System.IO.Stream ns = request.GetResponse().GetResponseStream();

               byte[] nbytes = new byte[byteCount];
               int nReadSize = 0;
               nReadSize = ns.Read(nbytes, 0, byteCount);
              
               while (nReadSize > 0)
               {
                   fs.Write(nbytes, 0, nReadSize);
                   nReadSize = ns.Read(nbytes, 0, byteCount);
                  
               }
               fs.Close();
               ns.Close();
           }
           catch(Exception ex)
           {
               LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
               fs.Close();
               result = false;
           }
           return result;     
       }
       #endregion 

--------------------编程问答-------------------- 我还要调用你这个函数?感觉比较复杂了。 --------------------编程问答-------------------- 请楼上给个调用方式? --------------------编程问答--------------------
private void DownLoadFile(string fileName)
        {
            string filePath = Server.MapPath("File")+"\\"+fileName;
            if (File.Exists(filePath))
            {
                FileInfo file = new FileInfo(filePath);
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
                Response.AddHeader("Content-length", file.Length.ToString());
                Response.ContentType = "appliction/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();
            }
        }
--------------------编程问答--------------------

String aPath = "C:\\boot.ini";
if (File.Exists(aPath) == true) {
this.Response.ContentType = Registry.GetValue("HKEY_CLASSES_ROOT\\" + aPath.Substring(aPath.LastIndexOf(".")).ToLower(), "Content Type", "application/octet-stream").ToString();

Byte[] aBytes = new Byte[0];

using (FileStream aStream = File.Open(aPath, FileMode.Open)) {
Array.Resize(ref aBytes, (Int32)(aStream.Length));
aStream.Read(aBytes, 0, (Int32)(aStream.Length));
this.Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(aPath.Substring(aPath.LastIndexOf("\\") + 1)));
this.Response.AddHeader("Content-length", aStream.Length.ToString());
}

this.Response.Clear();
this.Response.BinaryWrite(aBytes);
this.Response.End();
}

--------------------编程问答--------------------   /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileContext"></param>
        private void FileDownload(string fileName,byte[] fileContext)
        {
           using (Stream s = new MemoryStream(fileContext))
                {
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.ContentType = "application/octet-stream";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    Response.AppendHeader("Content-Length", fileContext.Length.ToString());

                    Response.BinaryWrite(fileContext); //二进制文件
                    Response.Flush();
                    Response.End();
                }
            }
        } --------------------编程问答-------------------- 怎么调用你写的函数,给个调用的方式?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,