등록 페이지에서 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();
}
}
하지만 런타임에 이미지를 표시하지 않습니다는
나에게
[이 링크] [1]을 참조하십시오. 귀하의 질문에 대한 답변은 바로 여기에 설명되어 있습니다. [1] : http://stackoverflow.com/questions/4665160/image-url-is-correct-but-image-not-showing 솔루션의 작동 방법 –