注册 登录
编程论坛 ASP.NET技术论坛

ASP.NET Response下载问题求助

xiaozheng001 发布于 2022-12-11 18:40, 728 次点击
问题大概是这样的,在原来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();
                }
            }

        }

--------------------------------
<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>

-------------------------------------------
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 });
        
    }
0 回复
1