下载时打开新窗口 [英] Open new window while download

查看:57
本文介绍了下载时打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以在其中下载文件的应用程序.我要在下载时打开新窗口,这意味着当用户单击下载按钮时,页面必须在另一页面上重定向,然后下载后便会开始.就像这个代码项目站点一样,当我们尝试下载任何文件时,它会将我们重定向到另一页上,然后开始下载.谁能帮我吗...??在此先感谢....

I have an application in which user can download the file. I want to open new window while downloading, I mean when user click on download button, page must be redirect on another page and then after download will start. Just like this code project site, when we try on download any file it''ll redirect us on another page and then after download will start. Can any one help me out...?? Thanks in advance....

推荐答案

下载链接指向一个新文档.在该文档上,您将创建一个javascript函数,该函数可以将用户重定向到实际的可下载文件.

Download.aspx:
Download link points to a new document. On that document you create a javascript-function which location-href''s the user to the actual downloadable file.

Download.aspx:
<body onload="location.href='fileToDownload.zip'">

  <p>Your download will start in a few seconds</p>

</body>



在下载图像的情况下,可以使用以下代码添加download.aspx:



In your case of downloading an image you can add a download.aspx with the following code:

protected void Page_Load(object sender, EventArgs e)
{
    // Get the full pathname for imagefile
    var imageFilename = Request.QueryString["imgName"];
    var path = Server.MapPath("/Images/" + imageFilename)

    // read the file
    using (FileStream fs = File.OpenRead(path))
    {
        int length = (int)fs.Length;
        byte[] buffer;

        using (BinaryReader br = new BinaryReader(fs))
        {
            buffer = br.ReadBytes(length);
        }

        // Force browser to download rather than open the file in browser
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(buffer);
    }
}





<body  önload="location.href='download.aspx?imgFile=nicepicture.jpg'">
 
  <p>Your download will start in a few seconds</p>
 
</body>


hi Naikniket760,

您可以使用target ="_ blank"在新页面中打开并下载.

kkakadiya
hi Naikniket760,

you can use target="_blank" to open in new page and download.

kkakadiya


尝试一下

Response.重定向到新窗口 [
try this

Response.Redirect into a new window[^]

may be this will help you
thank you
Chetanv


这篇关于下载时打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆