简体   繁体   English

下载后如何打开Word / Excel文件

[英]How to open Word/Excel file after downloading

I am using below code to download word/excel files. 我正在使用下面的代码下载word / excel文件。 How i can open this file automatically after downloading ? 下载后如何自动打开此文件?

private void DownloadFile(byte[] bArray, string mimeType, string KeyPath, bool bForcedownload)
        {
            Response.ContentType = mimeType;
            if (bForcedownload)
                Response.AppendHeader("Content-disposition",
                    "attachment; filename=" + Path.GetFileName(KeyPath));
            Response.AppendHeader("content-length", bArray.Length.ToString());
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.AppendHeader(@"Pragma", @"no-cache");
            Response.BinaryWrite(bArray);
            FlushSupressReponse();
        }

private void FlushSupressReponse()
        {
            HttpContext.Current.Response.Flush(); 
            HttpContext.Current.Response.SuppressContent = true;  
            HttpContext.Current.ApplicationInstance.CompleteRequest(); 
        }

Your comment: 你的评论:

We have UI which runs on browser , user click on filename and file gets downloaded...but user need to click on file to open it but i want to open it automatically once it is downloaded. 我们有在浏览器上运行的UI,用户单击文件名并下载了文件...但是用户需要单击文件将其打开,但我想在下载后自动将其打开。

You can't. 你不能 This is entirely the option of the user. 这完全是用户的选择。 You cannot force the user of a website to automatically open a file after downloading. 您不能强迫网站用户在下载后自动打开文件。

That is intentionally so, since it would be very easy to distribute viruses that way. 这是有意这样做的,因为这样很容易分发病毒。

However, you can give your users instructions on how to configure their browser to automatically open files of certain types. 但是,您可以为用户提供有关如何配置其浏览器以自动打开某些类型的文件的说明。 For example, in Chrome, after you download a file, there is an arrow beside the file name. 例如,在Chrome中,下载文件后,文件名旁边会有一个箭头。 Click on that and there is an option to "Automatically Open Files of This Type". 单击该选项,然后有一个选项“自动打开此类型的文件”。

But it's still up to your users to do it. 但这仍然取决于您的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM