我一直在努力解决问题一段时间,我正式放弃了。有人会帮我弄清楚以下代码有什么问题。我正在上传图片,然后我想显示它。
这是我的上传图片代码背后:
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
.aspx文件:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
然后要显示图像,我的Code Behind是:
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
我的.aspx是:
<asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Eval("ImageUrl") %>'/>
我验证了图片网址在数据库中,并且图片已保存到文件中。如果我在.aspx文件的图像控件中硬编码绝对路径,则会显示图片。但是,它不适用于上述'&lt;%#Eval(“ImageUrl”)%&gt;'码。我不知道问题出在哪里,所以任何帮助都会受到赞赏!
答案 0 :(得分:1)
问题是当您尝试保存图像时,
本声明
FileUpload1.SaveAs("C:/BegASPNET/test/Pictures/Profile/" + FileUpload1.FileName)
应该像......
FileUpload1.SaveAs(Server.MapPath("~/Pictures/Profile/") + FileUpload1.FileName)
答案 1 :(得分:0)
private string ConfigFileName = ConfigurationManager.AppSettings["FileName"];
string strAppPath = Request.PhysicalApplicationPath + "ProductImage";
string strImg = ConfigFileName + Request.Params["ModuleID"].ToString() + ".jpg";
fileupload1.PostedFile.SaveAs(strAppPath + "//" + strImg);