xiaozheng009 2022-12-11 18:26 采纳率: 0%
浏览 13

ASP.NET Reponse下载文件的问题

问题大概是这样的,在原来iis7.5,.NET2.0环境中,使用Reponse下载文件正常,在升级至IIS10,.NET4.5环境,发现下载的所有office文件都变大了,打开时并报文件错误,后在下载代码结束中增加了Reponse.flush(),Reponse.end(),下载的文件问理就解决了,但又引发了新的问题,文件正在下载时,其他的功能菜单点不开,须要等下载文件完成时才能打开,

private void DownloadLocal()
        {
            var fileId = Request.QueryString["FileId"];
            var relativeId = Request.QueryString["RelativeId"];
            var docEntity = new DocFileEntity(new Guid(fileId), new Guid(relativeId));

            if (!docEntity.IsNew)
            {
                string path = Server.MapPath("~/Files/" + docEntity.PhysicName);
                if (File.Exists(path))
                {
                    var userprofile = (IMS.Business.UserProfile)Session["UserProfile"];

                    //save history
                    var history = new FileDownloadHistoryEntity();
                    history.UserId = userprofile.UserID;
                    history.DocFileId = docEntity.FileId;
                    history.DownloadIp = Request.UserHostAddress;
                    history.Save();

                    string name = docEntity.LogicName;
                    if (string.IsNullOrEmpty(name))
                    {
                        name = new FileInfo(path).Name;
                    }
                    Response.AddHeader("Content-Disposition",
                            "attachment;filename=\"" + Server.UrlDecode(name) + "\"");
                    byte[] buffer = File.ReadAllBytes(path);
                    Response.AddHeader("Content-Length", buffer.Length.ToString());
                    Response.ContentType = "application/octet-stream;charset=utf-8";
                    Response.BinaryWrite(buffer);
                    Response.Flush();
                    Response.End();
 
                }
                }
        }

      
        private void DownloadByNetwork()
        {
            var fileId = Request.QueryString["FileId"];
            var relativeId = Request.QueryString["RelativeId"];
            var DBServiceUrl = ConfigurationManager.AppSettings["DBServiceUrl"];

            //查docfile 判断serverid 
            var docEntity = new DocFileEntity(new Guid(fileId), new Guid(relativeId));
            if (!docEntity.IsNew)
            {
                byte[] bytes = null;
                    
                var dbservice = new DBService(DBServiceUrl);
                if (docEntity.ServerId == (int) ServerIdEnums.Migration2017)
                {
                    bytes = dbservice.GetFileByPath2017(docEntity.PhysicName);
                }
                else
                {
                    bytes = dbservice.GetFileByFilePath(docEntity.PhysicName);
                }
             
                if (bytes != null)
                {
                    var userprofile = (IMS.Business.UserProfile) Session["UserProfile"];
                     
                    //save history
                    var history = new FileDownloadHistoryEntity();
                    history.UserId = userprofile.UserID;
                    history.DocFileId = docEntity.FileId;
                    history.DownloadIp = Request.UserHostAddress;
                    history.Save();

                    string name = docEntity.LogicName;
                    Response.AddHeader("Content-Disposition",
                        "attachment;filename=\"" + Server.UrlDecode(name) + "\"");
                    Response.AddHeader("Content-Length", bytes.Length.ToString());
                    Response.ContentType = "application/octet-stream;charset=utf-8";
                    Response.BinaryWrite(bytes);
        Response.Flush();
                    Response.End();
                }
            }

        }


------以下是页面上的调用--------
 function downloadFileById(fileId, relativeId) {
        if (!downloadFrame) {
            downloadFrame = $common.createElementFromTemplate({
                nodeName: 'IFRAME',
                visible: false
            }, document.body);
        }
        downloadFrame.src = Page.createUrl('<%=ResolveUrl("~/DownLoadFileById.aspx") %>', { FileId: fileId, RelativeId: relativeId });
        
    }
----
<ims:TemplateField HeaderText="Dnld" ItemStyle-Width="50px">
                                    <ItemStyle HorizontalAlign="Left" />
                                    <ItemTemplate>
                                        <div style="width: 50px;" class="divHidden">
                                            <asp:HyperLink ID="linkDownload" runat="server" Text="Dnld" ToolTip="Download this file."
                                                Visible="false"></asp:HyperLink>
                                            <img id="imgBtnDownload" src="../Images/download Files.gif" style="cursor: pointer"
                                                title="Download this file." onclick="javascript:downloadFileById('<%#Eval("FileId")%>','<%#Eval("RelativeId")%>');" />
                                        </div>
                                    </ItemTemplate>
                                </ims:TemplateField>

  • 写回答

1条回答 默认 最新

  • myshare2022 2022-12-16 00:30
    关注

    在最后加入应该可以解决:
    Context.ApplicationInstance.CompleteRequest();
    Response.Close();

    评论

报告相同问题?

问题事件

  • 修改了问题 12月11日
  • 创建了问题 12月11日

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序