| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 766 人关注过本帖
标题:ASP.NET Response下载问题求助
只看楼主 加入收藏
xiaozheng001
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-12-11
收藏
 问题点数:0 回复次数:0 
ASP.NET Response下载问题求助
问题大概是这样的,在原来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 });
        
    }
搜索更多相关主题的帖子: Response var name new Request 
2022-12-11 18:40
快速回复:ASP.NET Response下载问题求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017883 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved