Asked by:
how to display images in asp.net page from mssql database?

Question
-
User-1640542475 posted
Hi
I have an issue to display files with extension .jpeg/png/bmp/doc/docx/xls/xlsx
User upload multiple file format in to database. But below code is working fine for pdf only
How to do for others files format to display in asp.net web page.
Pls advice me
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""100%"" height=""500px"">" embed += "If you are unable to view file, you can download from <a href = ""{0}"">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(Me.txtFilePath.Text))
</div> <div>
It is my database table
CREATE TABLE [dbo].[CL_UPLoad_Document]( [id] [bigint] IDENTITY(1,1) NOT NULL, [DocNo] [varchar](20) NULL, [StaffCode] [varchar](20) NULL, [imgName] [varchar](100) NULL, [Path] [nvarchar](300) NULL, CONSTRAINT [PK_US_UPLoad_Docment] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
</div> <div>Pls advice me</div> <div>Maideen</div>Saturday, February 2, 2019 12:38 AM
All replies
-
User-943250815 posted
Ok you found a way to show a pdf on page.
To show an image file just use Image Control and set ImageURL property to right placce.
For Word & Excel files, best you can do is give a download link for userSaturday, February 2, 2019 1:11 AM -
User-893317190 posted
Hi maideen5,
You could get the extension of your file using Path.GetExtension(write your file name with extension) and show the file according to the file extension.
About word, I suggest you could use Aspose.Words,please use nuget to download it.It could easily convert word to pdf or html.
Document doc = new Document(Server.MapPath("/myy.docx")); doc.Save(Server.MapPath("/myyzzz.html"), SaveFormat.HtmlFixed);
Or you could use Microsoft.Office.Interop.Word to convert word to pdf.
https://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically
About xml,xlsx, you could use EPPlus to convert to excel to datatable and bind to gridview.
protected void Page_Load(object sender, EventArgs e) { DataTable table = new DataTable(); FileInfo info = new FileInfo(Server.MapPath("/my.xlsx")); ExcelPackage package = new ExcelPackage(info); //get the first sheet of excel, start with 1 not 0 ExcelWorksheet sheet = package.Workbook.Worksheets[1]; // sheet.Dimension.End.Column gets the column count of the excel sheet // Cells[1,1,1,sheet.Dimension.End.Column] means getting cells from // 1,1 to 1,sheet.Dimension.End.Column, which is all the cells in the first row foreach (var item in sheet.Cells[1,1,1,sheet.Dimension.End.Column]) { // add column to the datatable using the data of the first row in the excel sheet table.Columns.Add(new DataColumn(item.Text.Trim())); } //sheet.Dimension.End.Row get the row count of the sheet // loop through from the second row of the sheet, which contain data of the excel sheet for (int i = 2; i <= sheet.Dimension.End.Row ; i++) { //create a new row DataRow row = table.NewRow(); // loop through all the cells in the current row for (int j = 1; j <=sheet.Dimension.End.Column; j++) { // add data of every cell to the new datatable row row[sheet.Cells[1, j].Text.Trim()] = sheet.Cells[i, j].Text; } // add the row to datatable table.Rows.Add(row); } //new datatable has all the data from excel sheet // bind it to gridview GridView1.DataSource = table; GridView1.DataBind(); }
For more information for EPPlus, you could refer to https://riptutorial.com/epplus/example/27956/create-a-datatable-from-excel-file
Best regards,
Ackerly Xu
Monday, February 4, 2019 5:18 AM