Click here to Skip to main content
15,305,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<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>



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

VB.NET
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

SQL
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;


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
Posted
Updated 28-Dec-21 22:18pm
Comments
OriginalGriff 23-Jun-16 7:20am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And I have absolutely no idea what you are trying to do, or what happens when you run that code, or why any of this is a problem.
So slow down, and explain in simple words exactly what happens that you didn't expect, or doesn't happen that you did!
Use the "Improve question" widget to edit your question and provide better information.

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
C#
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();
}
   
Comments
Sergey Alexandrovich Kryukov 23-Jun-16 11:48am
   
5ed, but I think the inquirer needs to understand some PDF background. Also, your expression "no DIRECT way" is not quite accurate. Instead, you would need to explain that this is out of control of a Web application/site.

Please see Solution 2.

—SA
In addition to Solution 1.

"View PDF directly" is totally out of your control. This is the Web browser behavior a Web developer cannot affect in any way. Moreover, you should not assume that the client system has any software for viewing PDF at all. This is just not your responsibility; and it does not present any practical problems.

PDF is not a part of W3 standards. The fact that you may often see PDF viewed as a browser page means nothing. This is usual, but not a standard behavior. Most usually, this is achieved via some browser plug-in. If there is no such software, the user will be given a choice to open document in a default viewer or save it in a local file. That default viewer may also be not installed, but this is also not a problem, because the user can just save the file and view it later, using a non-default viewer, install some software later, view on a different system, and so on.

In your Web design, you should never assume that any of the ways described above is used. It's totally up to the user.

—SA
   
Comments
koolprasadd 23-Jun-16 23:53pm
   
+5. '"View PDF directly" is totally out of your control' so true.
Good reply SA.
   
Thank you, Prasad.
—SA
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


how to use this in c# language
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900