PDF文件将直接查看我不需要下载 [英] PDF file will be view on directly I dont need download

查看:112
本文介绍了PDF文件将直接查看我不需要下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div>
       <asp:FileUpload ID="FileUpload1" runat="server" />
       <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
       <hr />
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
           <Columns>
               <asp:BoundField DataField="Name" HeaderText="File Name" />
               <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                   <ItemTemplate>
                       <asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
       <hr />
       <div>
           <asp:Literal ID="ltEmbed" runat="server" />
       </div>
   </div>










<pre lang="vb"><pre lang="vb">




Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports Framework

Public Class dap_trn_documentupload
    Inherits System.Web.UI.Page
    Dim objdbconn As New dbconn
    Dim msSQL As String
    Dim ds_users As New DataSet
    Dim mnResult As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGrid()
        End If
    End Sub
    Private Sub BindGrid()
        objdbconn.OpenConn()
        msSQL = "select Id, Name from tblFiles"
        ds_users = objdbconn.GetDataSet(msSQL, "tblFiles")
        GridView1.DataSource = ds_users
        GridView1.DataMember = "tblFiles"
        GridView1.DataBind()
        objdbconn.CloseConn()
    End Sub

    Protected Sub Upload(sender As Object, e As EventArgs)
        Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
        Dim contentType As String = FileUpload1.PostedFile.ContentType
        Using fs As Stream = FileUpload1.PostedFile.InputStream
            Using br As New BinaryReader(fs)
                Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Long))
                objdbconn.OpenConn()
                msSQL = ""
                msSQL &= "insert into tblFiles (Name,ContentType) "
                msSQL &= " values('" & filename & "','" & contentType & "')"
                mnResult = objdbconn.ExecuteNonQuerySQL(msSQL)
                objdbconn.CloseConn()
            
                End Using
            End Using

        Response.Redirect(Request.Url.AbsoluteUri)
    End Sub

   



End Class









DB







DB

DROP TABLE IF EXISTS `tabtree_new`.`tblfiles`;
CREATE TABLE  `tabtree_new`.`tblfiles` (
  `Id` int(10) unsigned NOT NULL auto_increment,
  `Name` varchar(45) NOT NULL,
  `ContentType` varchar(200) NOT NULL,
  `Data` varbinary(5000) default NULL,
  PRIMARY KEY  USING BTREE (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;





我的尝试:



受保护的子视图(ByVal sender As Object,ByVal e As EventArgs)

Dim id As Integer = Integer.Parse(TryCast) (sender,LinkBut​​ton).CommandArgument)

Dim embed As String =< object data ={0} {1}type =application / pdfwidth = 500pxheight =600px>

embed + =如果您无法查看文件,可以从< a href ={0} {1}下载& download = 1> here< / a>

embed + =或者下载< a target =_ blankhref =http://get.adobe .com / reader /> Adob​​e PDF Reader< / a>查看文件。

embed + =< / object>

ltEmbed.Text = String.Format(embed,ResolveUrl(〜/ ems_dailyprocess / pdf -sample.pdf),id)

结束子



我在这段代码中犯了什么错误我不知道



如果文件上传和值应该在网格上设置是成功的当我点击特定的Pdf我需要查看特定的文件我应该写的任何1可以帮助我给关于那段代码的想法它真的帮助了我的知识



What I have tried:

Protected Sub View(ByVal sender As Object, ByVal e As EventArgs)
Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
Dim embed As String = "<object data=""{0}{1}"" type=""application/pdf"" width=""500px"" height=""600px"">"
embed += "If you are unable to view file, you can download from <a href = ""{0}{1}&download=1"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/ems_dailyprocess/pdf-sample.pdf"), id)
End Sub

what shoul i hav mistake in this code i dont no

if the file upload and value should be set on grid is sucessfully when i click the particular Pdf i need to view that particular File what i should writen can any 1 help me to give the idea about that code its really help me for my knowledge

推荐答案

你想要查看该文件吗?

没有DIRECT方式在基于Web的应用程序(asp.net)中查看您上传的文件,您需要下载并查看。

您可以响应对象,请参阅下面的代码片段

You want to view that file right ?
There is no DIRECT way to view your uploaded files in web based application (asp.net) you need to download it and view.
for that you can 'Response' object, see below snippet for sample
protected void DownloadFile(object sender, EventArgs e)
{
//pass filepath
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}


除了解决方案1.



直接查看PDF完全不受你的控制。这是Web开发人员不能以任何方式影响的Web浏览器行为。此外,您不应该假设客户端系统有任何用于查看PDF的软件。这不是你的责任;并且它没有任何实际问题。



PDF不是W3标准的一部分。您经常看到PDF被视为浏览器页面这一事实毫无意义。这是常见的,但不是标准行为。最常见的是,这是通过一些浏览器插件实现的。如果没有此类软件,则可以选择在默认查看器中打开文档或将其保存在本地文件中。也可能没有安装该默认查看器,但这也不是问题,因为用户可以稍后保存文件并查看,使用非默认查看器,稍后安装某些软件,在不同系统上查看等等on。



在您的网页设计中,您绝不能假设使用上述任何方式。这完全取决于用户。



-SA


这篇关于PDF文件将直接查看我不需要下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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