Asp.Net image will not display
I have been trying to troubleshoot for a while now and I officially give up. Would someone help me figure out what is wrong with the following code. I am uploading an image and then I want to display it.
This is my upload image Code Behind:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs("C:/BegASPNET/test/Pictures/Profile/" + FileUpload1.FileName)
Label1.Text = "File name: " + FileUpload1.PostedFile.FileName
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString()
End Try
Else
Label1.Text = "You have not specified a file."
End If
Using myEntities As New DatabaseEntities()
Dim pic As Picture
pic = New Picture()
pic.UserID = Profile.ID
pic.ImageUrl = "~/Pictures/Profile/" + FileUpload1.FileName
myEntities.AddToPictures(pic)
myEntities.SaveChanges()
End Using
End Sub
And the .aspx file:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
And then to display the image, my Code Behind is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Using myProfile As New DatabaseEntities()
Dim prof = (From p In myProfile.UserProfiles
Where p.UserID = Profile.ID
Select p).SingleOrDefault()
dFNameLabel.Text = prof.FirstName
dLNameLabel.Text = prof.LastName
dDOBLabel.Text = prof.DOB
dGenderLabel.Text = prof.Gender
dEmailLabel.Text = prof.Email
Dim pic = (From pi In myProfile.Pictures
Where pi.UserID = Profile.ID AndAlso pi.picDefault = True
Select pi.ImageUrl).SingleOrDefault
End Using
End Sub
And my .aspx is:
<asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
I verified that the image url is in the database and that the image is saved to a file. If I hard code an absolute 开发者_运维知识库path in the image control of the .aspx file the picture will display. But, it won't work with the above '<%# Eval("ImageUrl") %>' code. I have no idea where the problem lies so any help would be appreciated!
Problem is when you are trying to save the image,
This statement
FileUpload1.SaveAs("C:/BegASPNET/test/Pictures/Profile/" + FileUpload1.FileName)
should be like...
FileUpload1.SaveAs(Server.MapPath("~/Pictures/Profile/") + FileUpload1.FileName)
private string ConfigFileName = ConfigurationManager.AppSettings["FileName"];
string strAppPath = Request.PhysicalApplicationPath + "ProductImage";
string strImg = ConfigFileName + Request.Params["ModuleID"].ToString() + ".jpg";
fileupload1.PostedFile.SaveAs(strAppPath + "//" + strImg);
精彩评论