如何在下载过程中打开文件内容 [英] How to open File content while downloading in progress

查看:108
本文介绍了如何在下载过程中打开文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我们需要增强文档下载功能,在我的asp.net应用程序中,我可以选择下载存储在SQL Server DB中的文档(不在服务器文件系统中)。目前点击下载链接,我们需要等到整个文档下载,然后只有我们能够看到文档,这个功能正常工作,但我们需要改变这种方法,以获得更好的用户体验,并希望看到记录到目前为止下载的内容。这意味着在下载时我们需要打开正在下载的文件。

目前正在下载链接点击我的代码看起来像这样..



Hi friends,

We need to enhance our document download functionality,In my asp.net application i have option to download document which is stored in SQL Server DB (Not in Server File System). currently on click of download link, we need to wait till the whole document is downloaded, then only we can able to see the document, this functionality is working properly but we need to change this approach for better experience of users and want to see the document whatever downloaded so far.it means that while downloading itself we need to open document which is being downloaded.
currently on download link click my code looks like this..

string fileName = Request.QueryString["name"].ToString();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/FilesToDownload/" + fileName));
Response.End();





任何帮助都将不胜感激。



在此先感谢.. !!



Any Help Regarding this would be appreciated.

Thanks In Advance..!!

推荐答案

您可以像这样满足您的要求,



1 )您将下载链接传递给新选项卡而不是当前选项卡。



2)在当前选项卡中通过Google文档打开您的文档。



You can achive your requirement like this,

1) you pass your download link to new tab not in current tab.

2) In current tab open your doc through the google docs.

1. Open document using iframe. 

    Inline frame or iframe is a window cut into your webpage that allows you to view another page on your site. It gives you the flexibility to render a specific portion without reloading entire page.

    ok. using this iframe you can view a document file including word, excel, power-point, pdf, text file and many more. One thing you need to remember that you must have document reader, installed at your client PC to open and read the document.

lets see how we can achieve this.
I assume that there is a folder FileStore at your website which holds all the document files.

1. add a new page.
2. at aspx page add an iframe.

        <iframe id="iframShowFiles" runat="server" height="600" width="800" frameborder="0">
        </iframe>

3. at cs page add the following code at page load event-

string fileName = Request.QueryString["FileName"] != null ? Request.QueryString["FileName"].ToString() : string.Empty;
        string surverUrl = Request.Url.AbsoluteUri.Split('/')[0] + "//" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
        string fileStoreLocation = "FileStore/";
        string downloadFilePath = surverUrl + fileStoreLocation + fileName;

        iframShowFiles.Attributes.Add("src", downloadFilePath);


now at the target aspx page add a link -

<a target="_blank" href="Default.aspx?FileName=Project-Food.xlsx">View file using iframe</a>

now tun the site.



2. Open document using HTTPResponse. 

follow the following instruction:

1. In target aspx page add a hyperlink -

<asp:HyperLink ID="hypDownloadMyFile" runat="server" Text="View file using HTTPResponse"
            NavigateUrl=""></asp:HyperLink>

2. at cs page write the following code at page load event -

        string fileName = "Project-Food.xlsx";
      
        #region Download file using HTTpResponse

        hypDownloadMyFile.Attributes.Add("onclick", "javascript:DownloadFile();");
        hypDownloadMyFile.Attributes.Add("onmouseover", "this.style.cursor='pointer';this.style.color='red';");
        hypDownloadMyFile.Attributes.Add("onmouseout", "this.style.color='';");
        if (!Page.IsPostBack)
        {
            bool isDownloadFile = false;
            if (Request.QueryString["DownloadFile"] != null && Request.QueryString["DownloadFile"] == "1")
                isDownloadFile = true;

            if (isDownloadFile)
            {
                DownloadMyFile(fileName);
            }
        }

        #endregion


and this is DownloadMyFile method -


 private void DownloadMyFile(string fileName)
    {
        string fileStoreLocation = "~/FileStore/";
        string physicalPath = Server.MapPath(fileStoreLocation);
        string filePath = physicalPath + fileName;
        if (System.IO.File.Exists(filePath))
        {
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.WriteFile(filePath);
            Response.End();
        }
    }


now run the site.




3. Open document using Google doc viewer. 

   This is very interesting thing. Google introduces this document viewer. You do not need any document reader installed at client pc to open and read the document. The supported file types are:
Microsoft Word (.DOC and .DOCX)
Microsoft Excel (.XLS and .XLSX)
Microsoft PowerPoint (.PPT and .PPTX)
Adobe Portable Document Format (.PDF)
Apple Pages (.PAGES)
Adobe Illustrator (.AI)
Adobe Photoshop (.PSD)
Tagged Image File Format (.TIFF)
Autodesk AutoCad (.DXF)
Scalable Vector Graphics (.SVG)
PostScript (.EPS, .PS)
TrueType (.TTF)
XML Paper Specification (.XPS)
Archive file types (.ZIP and .RAR)
as per google Technical Documentation - Instructions for building your own URLs

All viewer URLs should use the path http://docs.google.com/viewer .
This path accepts two parameters:
1. url : The URL of the document to view. This should be URL-encoded.
2. embedded : If set to true , the viewer will use an embedded mode interface.

For example, if you wanted to view the PDF at the URL http://research.google.com/archive/bigtable-osdi06.pdf , you would use the URL:

http://docs.google.com/viewer?url=http%3A%2F%2Fresearch.google.com%2Farchive%2Fbigtable-osdi06.pdf

lets use a real life example:

1. Add a hyperlink into your target aspx page -

<asp:HyperLink ID="hypViewFileUsingGoogleDoc" runat="server" Text="View file using Google Doc"
            Target="_blank"></asp:HyperLink>

2. add the following code at cs page at page load event -

 #region Download file using Google Doc 
    
        hypViewFileUsingGoogleDoc.Attributes.Add("onmouseover", "this.style.cursor='pointer';this.style.color='red';");
        hypViewFileUsingGoogleDoc.Attributes.Add("onmouseout", "this.style.color='';");
        string surverUrl = Request.Url.AbsoluteUri.Split('/')[0] + "//" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
        string fileStoreLocation = "FileStore/";
        string downloadFilePath = surverUrl + fileStoreLocation + fileName;
        hypViewFileUsingGoogleDoc.NavigateUrl = "http://docs.google.com/viewer?url=" + downloadFilePath;

        #endregion

now run the site.

If you found a screen like this -

 


Do not worry about this. Google cannot format and display the document from private file storage that are behind a firewall or on a local site that is accessed with a 'localhost' address.

Click on red font 'here' text. the document will download.

You can discover many more from the Google doc viewer.


这篇关于如何在下载过程中打开文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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