簡體   English   中英

更改內容處置以在新選項卡中打開所有圖像文件文件

[英]Change Content-Disposition to open all image file files in a new tab

這是下載文件的邏輯。

它會自動下載文件,無論是.jpg,.pdf,.zip還是其他文件,只要它是圖像文件(jpg,png等),我都希望它僅在新選項卡中打開,然后其他所有提示下載。

在解決這個問題時,我感覺好像缺少了一些東西。 :/

public void DownloadArchivedFiles(ArchiveType type, Object id, String fileName)
    {
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + fileName);

        String path = Server.MapPath(String.Format("~/{0}Files/{1}", type, id));
        var fmFiles = new string[0];
        var files=new string[0];
        if(type==ArchiveType.Issue)
        { 
            fmFiles = _files.GetIssueFiles(new Guid(id.ToString())).Select(x => Server.MapPath("~" + x.FilePath)).ToArray();
        }else if(type==ArchiveType.Task)
        {
            fmFiles = _files.GetTaskFiles(int.Parse(id.ToString())).Select(x => Server.MapPath("~" + x.FilePath)).ToArray();
        }
        if (!System.IO.Directory.Exists(path) && !fmFiles.Any()) return;

        //String[] files = System.IO.Directory.GetFiles(Server.MapPath(String.Format("~/{0}Files/{1}", type, id)));
        try
        {
            files = System.IO.Directory.GetFiles(Server.MapPath(String.Format("~/{0}Files/{1}", type, id)));
        }
        catch (Exception)
        {
            ;
        }

        using (ZipFile zip = new ZipFile())
        {
            zip.AddFiles(files, "/");
            if(fmFiles.Any())
            zip.AddFiles(fmFiles,"/");
            zip.Save(Response.OutputStream);
        }
        HttpContext.Response.End();
    }

您無法通過在響應中提供標題來打開新標簽頁。 太晚了。 在生成響應時,瀏覽器已經在嘗試將響應流放入同一選項卡,並且舊文檔可能已經被卸載。

相反,您需要更改發送請求的方式。 具體來說,圖像的鏈接必須具有_target="blank"作為屬性。 這告訴瀏覽器打開一個新的選項卡(或窗口,取決於用戶的瀏覽器設置),然后在那里請求圖像。 此時,您不需要對響應頭做任何特殊的事情。

對於可下載文件(例如PDF),應避免使用target="blank" 在某些瀏覽器上可以正常工作,但在其他瀏覽器上,您將得到難看的僵屍標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM