Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 17, 2012 02:56 PM by masoud-s
Member
81 Points
196 Posts
Nov 17, 2012 02:56 PM|LINK
before i have a question about (icon of uploaded file in gridview) and recieved below code.
this code seems that is for when we save files in the server. now if we save files in database how i have to change the code.
using System.Drawing; using System.IO; public string GetIconFromFile() { Icon ic = Icon.ExtractAssociatedIcon(Server.MapPath (".")+"/Files/Test.txt"); string imagePath=Server.MapPath(".") + "/Images/Test.ico"; if (ic != null) { using (FileStream stream = new FileStream(imagePath, FileMode.OpenOrCreate)) { ic.Save(stream); } } return imagePath ; } protected void GridViewEfile_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); img = (System.Web.UI.WebControls.Image)e.Row.FindControl("Image1"); img.ImageUrl = GetIconFromFile(); } }
aspx
<Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" /> <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton> </ItemTemplate> </asp:TemplateField>
the code for uploading file is like below:
protected void btnUpload_Click(object sender, EventArgs e) { // Read the file and convert it to Byte Array string filePath = FileUpload1.PostedFile.FileName; string filename = Path.GetFileName(filePath); string ext = Path.GetExtension(filename); string contenttype = String.Empty; //Set the contenttype based on File Extension switch (ext) { case ".doc": contenttype = "application/vnd.ms-word"; break; case ".docx": contenttype = "application/vnd.ms-word"; break; case ".xls": contenttype = "application/vnd.ms-excel"; break; case ".xlsx": contenttype = "application/vnd.ms-excel"; break; case ".jpg": contenttype = "image/jpg"; break; case ".png": contenttype = "image/png"; break; case ".gif": contenttype = "image/gif"; break; case ".pdf": contenttype = "application/pdf"; break; } if (contenttype != String.Empty) { Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); ....
masoud-s
Member
81 Points
196 Posts
icon of uploaded file in gridview when file has been saved in database
Nov 17, 2012 02:56 PM|LINK
before i have a question about (icon of uploaded file in gridview) and recieved below code.
this code seems that is for when we save files in the server. now if we save files in database how i have to change the code.
using System.Drawing; using System.IO; public string GetIconFromFile() { Icon ic = Icon.ExtractAssociatedIcon(Server.MapPath (".")+"/Files/Test.txt"); string imagePath=Server.MapPath(".") + "/Images/Test.ico"; if (ic != null) { using (FileStream stream = new FileStream(imagePath, FileMode.OpenOrCreate)) { ic.Save(stream); } } return imagePath ; } protected void GridViewEfile_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); img = (System.Web.UI.WebControls.Image)e.Row.FindControl("Image1"); img.ImageUrl = GetIconFromFile(); } }aspx
<Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" /> <asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="Download" CommandArgument='<%#Eval("FileID")%>'><%#Eval("FileName")%></asp:LinkButton> </ItemTemplate> </asp:TemplateField>the code for uploading file is like below:
protected void btnUpload_Click(object sender, EventArgs e) { // Read the file and convert it to Byte Array string filePath = FileUpload1.PostedFile.FileName; string filename = Path.GetFileName(filePath); string ext = Path.GetExtension(filename); string contenttype = String.Empty; //Set the contenttype based on File Extension switch (ext) { case ".doc": contenttype = "application/vnd.ms-word"; break; case ".docx": contenttype = "application/vnd.ms-word"; break; case ".xls": contenttype = "application/vnd.ms-excel"; break; case ".xlsx": contenttype = "application/vnd.ms-excel"; break; case ".jpg": contenttype = "image/jpg"; break; case ".png": contenttype = "image/png"; break; case ".gif": contenttype = "image/gif"; break; case ".pdf": contenttype = "application/pdf"; break; } if (contenttype != String.Empty) { Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); ....