简体   繁体   English

Asp.Net图像将不会显示

[英]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: 和.aspx文件:

<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: 我的.aspx是:

<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. 我验证了图像URL在数据库中,并且图像已保存到文件中。 If I hard code an absolute path in the image control of the .aspx file the picture will display. 如果我对.aspx文件的图像控件中的绝对路径进行硬编码,则将显示图片。 But, it won't work with the above '<%# Eval("ImageUrl") %>' code. 但是,它不适用于上面的'<%#Eval(“ ImageUrl”)%>'代码。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM