2014-05-21 104 views
1

我在注册页面中的asp.net中创建了一个应用程序我想上传该页面上的图片并同时保存在数据库中,但数据库工作已经完成了我能够保存我的上传图像,但是无法在该页面上显示。在asp.net中上传时显示图像

我的设计代码:

<form id=`form 1` run at="server"> 
    <div> 
    <table> 
    <tr> 
    <td> 
     <asp:FileUpload ID="FileUpload1" run at="server" /> 
    </td> 
    <td> 
     <asp:Button ID="Button1" runat="server" Text="submit" onclick="Button1_Click" /></td> 
     <td> 
      <asp:Label ID="Label1" runat="server" Text="" Font-Names = "Arial"></asp:Label> 
      <asp:Image ID="Image1" runat="server" Height="52px" Width="79px" /> 
     </td></tr></table> 

    </div> 
    </form> 

我后面的代码:

protected void Button 1_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; 
     Image1.ImageUrl = filename; 
     //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 
      string strQuery = "insert into empimage(Name, ContentType, Data)" + 
       " values (@Name, @ContentType, @Data)"; 
      SqlCommand cmd = new SqlCommand(strQuery); 
      cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename; 
      cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value 
       = contenttype; 
      cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; 
      InsertUpdateData(cmd); 
      Label1.ForeColor = System.Drawing.Color.Green; 
      Label1.Text = "File Uploaded Successfully"; 
     } 
     else 
     { 
      Label1.ForeColor = System.Drawing.Color.Red; 
      Label1.Text = "File format not recognised." + 
       " Upload Image/Word/PDF/Excel formats"; 
     } 

} 
    private Boolean InsertUpdateData(SqlCommand cmd) 
    { 
     pd = ConfigurationManager.ConnectionStrings["su"].ConnectionString; 
     con = new SqlConnection(pd); 
     //String strConnString = System.Configuration.ConfigurationManager 
     //.ConnectionStrings["conString"].ConnectionString; 
     //SqlConnection con = new SqlConnection(strConnString); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     try 
     { 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
      return false; 
     } 
     finally 
     { 
      con.Close(); 
      con.Dispose(); 
     } 
    } 

,但它并没有显示在运行时图像

请建议我

+0

请参考[此链接] [1]。在这里描述你的问题的答案。 [1]:http://stackoverflow.com/questions/4665160/image-url-is-correct-but-image-not-showing简要 –

回答

0

你必须尝试像

Image1.ImageUrl = FileUpload1.FileName; 
+1

能否请您解释一下您的解决方案是如何工作的。这会让你的回答对OP和其他读者更有帮助(+ 1s)。 –

+0

我试试这个代码,但它不工作它只显示图像图标,但不是图像 但我找到了我的问题的答案 代码是 protected void Button1_Click(object sender,EventArgs e) {if(FileUpload1.PostedFile!= null && FileUpload1.PostedFile.FileName!=“”) {FileUpload1.SaveAs(Server.MapPath(“image /”+ FileUpload1.FileName)); Image1.ImageUrl =“〜/ image /”+ FileUpload1.FileName; (插入到empimage(名称)值('“+”〜/ Image /“+ FileUpload1.FileName +”')“,con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } – user3627833

+0

好开心编码 –