使用asp.net在浏览器中打开任何类型的文件(.doc,docx,.txt,.jpg,.jpeg,.pdf等) [英] opening any type of file (.doc,docx,.txt,.jpg,.jpeg,.pdf etc) in the browser using asp.net

查看:88
本文介绍了使用asp.net在浏览器中打开任何类型的文件(.doc,docx,.txt,.jpg,.jpeg,.pdf等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net中编码,当用户单击重定向到文件的链接(例如"sample.doc")时,必须在浏览器中打开该文件.我能够在单独的窗口中打开.MY目的是在浏览器本身中打开

就像我们如何查看gmail附件中的任何类型的文档一样.

I want to code in asp.net that when ever user clicks on link which redirects to file say "sample.doc" ,that file has to be opened in the browser.I am able to open in the separate window.MY intention is to open in the Browser itself

It just like how we are able to view any type of document in the gmail''s attachment

推荐答案

尝试此链接..
希望对您有帮助
Try this link..
hope it can help
tutorial


嗨普拉萨德,

这完全取决于您发送给用户的响应类型.响应包括有关正在发送的数据类型和数据本身的一些标头详细信息,然后浏览器读取信息并采取相应的操作.

例如:
Hi Prasad,

it all depends on what type of RESPONSE you send to user. The response includes some header details on type of data being sent and the data itself, the browser then, reads the information an takes action accordingly.

For example:
Dim stream As New MemoryStream
   document.Save(stream, False)
        Response.Clear()
        Response.ContentType = "application/excel"
        Response.AddHeader("content-length", stream.Length.ToString())
        Response.BinaryWrite(stream.ToArray())
        Response.Flush()
        stream.Close()
        Response.End()



上面的代码表示传递的数据是excel应用程序的类型.然后浏览器向操作系统询问诸如excel之类的应用程序,或者是否给出了任何扩展名,浏览器会询问注册了该扩展名的应用程序并在该应用程序中打开它.

因此,您需要发送适当的标头信息以及要实现的数据.

尝试添加以下语句:



The above code says the data being passed is type of excel application. then browser asks the OS for application like excel or if any extension is given the browser asks for the application with which extension is registered and open it in that application.

So you need to send proper header information with data for what you want to achieve.

try adding following statement:

Response.AppendHeader("Content-Disposition", "inline;filename=" + "ExcelFile.xls")



如果您有像Google这样的工作示例,则可以检查google发送给浏览器的确切文件以打开文件.您可以使用
Fiddler 查看浏览器向google发送的所有请求以及得到的响应是什么.您可以根据自己的目的使用相同的设置.

希望这会有所帮助.

谢谢



If you have a working example like google, you can check what exactly google is sending to your browser to open the file. You can use Fiddler to see what all requests the browser is sending for google and what is the response it is getting. The same settings you can use for your purpose.

Hope this will help.

Thanks,


这篇关于使用asp.net在浏览器中打开任何类型的文件(.doc,docx,.txt,.jpg,.jpeg,.pdf等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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