2014-05-21 2 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와 다른 독자들에게 답이 도움이 될 것입니다 (+1). –

+0

(개체를 보낸 사람있는 EventArgs e)를 코드 무효를 Button1_Click을 보호 나는이 코드를 시도하지만 그것은 단지 이미지 아이콘이 아닌 이미지 을 보여 작동하지 않습니다하지만 난 내 질문 의 답을 찾을 수 {경우 (FileUpload1.PostedFile! = null && FileUpload1.PostedFile.FileName! = "") {FileUpload1.SaveAs (Server.MapPath ("image /"+ FileUpload1.FileName)); Image1.ImageUrl = "~/image /"+ FileUpload1.FileName; } cmd = 새 SqlCommand ("empimage (Name) 값에 삽입하십시오 (" "+"~/Image/"+ FileUpload1.FileName +" ') ", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } – user3627833

+0

행복한 만족스러운 코딩 –

관련 문제