0

(グリッドビューでアップロードされたファイルのアイコン)について質問し、コードの下で受け取る前に。

このコードは、サーバーにファイルを保存するときのもののようです。データベースにファイルを保存する場合、コードを変更する必要があります。

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>

データベースに保存したいときのアップロードボタンのコードは以下のようなものです。関数 (GetIconFromFile()) のどのような変更を助けてください。

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);
        //insert the file into database
4

1 に答える 1

0

コードを記述する必要がある関数GetIconFromFileを変更する必要があります

  1. アイコンファイルをデータベースに保存しています。

    画像の保存については、これを参照してください

  2. データベースからグリッドビューに画像を表示する

    グリッドビューでの表示については、これを参照してください

于 2012-11-17T16:06:23.870 に答える