2017-04-16 20 views
0

我有一個模塊將文件上傳到我的sqlserver數據庫,其中我有像FName字段:varchar文件名,ContentType:nvarchar FileType和一個Data:varbinary實際數據。所以現在我可以使用BoundField來檢索它,它只顯示我上傳文件的名稱,但我真正想要的是從數據庫中識別文件,如果它是圖像文件,那麼它應該顯示該圖像在gridview中,如果它是一個非圖像文件,那麼讓它只顯示文件名。注意我不想使用文件夾上傳文件並使用文件路徑檢索文件。會有人也有類似的經驗分享,請:如何在不使用文件路徑的情況下在gridview中從數據庫中退出圖像

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" 
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" 
    AutoGenerateColumns="false"> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="File Name"/> 
     <asp:TemplateField ItemStyle-HorizontalAlign = "Center"> 
      <ItemTemplate> 
       <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile" 
        CommandArgument='<%# Eval("Id") %>'></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

代碼背後:

protected void Upload(object sender, EventArgs e) 
{ 
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); 
    string contentType = FileUpload1.PostedFile.ContentType; 
    using (Stream fs = FileUpload1.PostedFile.InputStream) 
    { 
     using (BinaryReader br = new BinaryReader(fs)) 
     { 
      byte[] bytes = br.ReadBytes((Int32)fs.Length); 
      string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
      using (SqlConnection con = new SqlConnection(constr)) 
      { 
       string query = "insert into tblFiles values (@Name, @ContentType, @Data)"; 
       using (SqlCommand cmd = new SqlCommand(query)) 
       { 
        cmd.Connection = con; 
        cmd.Parameters.AddWithValue("@Name", filename); 
        cmd.Parameters.AddWithValue("@ContentType", contentType); 
        cmd.Parameters.AddWithValue("@Data", bytes); 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        con.Close(); 
       } 
      } 
     } 
    } 

} 
+0

你有內容類型的權利?你爲什麼不用它來決定展示什麼? – Krishna

+0

@Krishna好的,我需要綁定/ eval在itemtemplate或我該怎麼做,你能精確嗎? –

回答

1

您需要使用的ContentType來決定如何顯示數據

<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" 
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" OnRowDataBound="GridView1_RowDataBound" 
    AutoGenerateColumns="false"> 
    <Columns> 
     <asp:BoundField DataField="Name" HeaderText="File Name"/> 
     <asp:TemplateField ItemStyle-HorizontalAlign = "Center"> 
      <ItemTemplate> 
       <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="DownloadFile" 
        CommandArgument='<%# Eval("Id") %>'></asp:LinkButton> 
       <img runat="server" id="imgData" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
在行數據

束縛決定做什麼

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        LinkButton lnkButton = (LinkButton)e.Row.Cells[1].FindControl("lnkDownload"); 
        HtmlImage img = (HtmlImage)e.Row.Cells[1].FindControl("imgData"); 
        if (DataBinder.Eval(e.Row.DataItem, "ContentType").ToString().Contains("image"))//like image/jpeg 
        { 
         lnkButton.Visible = false; 
         img.Visible = true; 
         img.Src = "data:image/png;base64," + Convert.ToBase64String((byte[])DataBinder.Eval(e.Row.DataItem,"Data")); 
        } 
        else 
        { 
         lnkButton.Visible = true; 
         img.Visible = false; 
        } 
       } 
      } 
+0

它不顯示圖像也不會引發錯誤。下一步是什麼? –

+0

@DharamRai它是否使圖像在行數據綁定中可見?你調試了嗎?發生了什麼 ? – Krishna

+0

好吧,它現在工作完美。感謝您分享您的學習。 –

相關問題