zoukankan      html  css  js  c++  java
  • 下载文件时提示"无法复制文件,无法读取文件或磁盘"的错误解决方案

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;

    namespace infoPlatClient.NetDisk
    {
        public partial class downLoad : Com.DRPENG.Common.WebStruct.BaseForm
        {

            /// <summary>
            /// 取得要下载文件的路径
            /// </summary>
            private string fileRpath
            {
                get
                {
                    return Request["fileRpath"] == null ? "" : Request["fileRpath"];
                }
            }
            /// <summary>
            /// 取得要下载文件的名称
            /// </summary>

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    DownLoad();
            }


            private void DownLoad()
            {
                string name = System.IO.Path.GetFileName(fileRpath);
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileRpath);
                if (fileInfo.Exists == true)
                {
                    const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
                    byte[] buffer = new byte[ChunkSize];

                    Response.Clear();
                    System.IO.FileStream iStream = System.IO.File.OpenRead(fileRpath);
                    long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                    Response.ContentType = "application/octet-stream;charset=gbk";
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                    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;
                    }
                    Response.Close();
                }
            }

        }
    }
    主要是这段代码:  Response.ContentType = "application/octet-stream;charset=gbk";

  • 相关阅读:
    Hibernate:组合模式解决树的映射
    以面到点的学习MFC
    linux内核--进程与线程
    控件自定义
    火车车次查询-余票查询--Api接口
    如何处理大量数据并发操作(数据库锁机制详解)
    Java单链表、双端链表、有序链表实现
    事务、数据库事务、事务隔离级别、锁的简单总结
    数据库连接池分析
    Spring面试题集
  • 原文地址:https://www.cnblogs.com/zhangzt/p/1640463.html
Copyright © 2011-2022 走看看