2011-07-30 181 views
0

我一直在嘗試排除故障一段時間,我正式放棄。有人會幫我弄清楚下面的代碼有什麼問題。我正在上傳圖片,然後我想要顯示它。Asp.Net圖像不會顯示

這是我上傳的圖片中的代碼背後:

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"/> 

然後顯示圖像,我的代碼背後是:

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") %>'/> 

我確認圖像url位於數據庫中,並且圖像保存在文件中。如果我在.aspx文件的圖像控件中硬編碼絕對路徑,圖片將顯示。但是,它不會與上面的'<%#Eval(「ImageUrl」)%>'代碼一起使用。我不知道問題出在哪裏,所以任何幫助將不勝感激!

回答

1

問題是當你試圖保存圖像,

本聲明

FileUpload1.SaveAs("C:/BegASPNET/test/Pictures/Profile/" + FileUpload1.FileName) 

應該是這樣......

FileUpload1.SaveAs(Server.MapPath("~/Pictures/Profile/") + FileUpload1.FileName) 
+0

感謝穆罕默德!我嘗試了你的建議,但問題依然存在。 –

+0

您在ImageURL中獲得了什麼? –

+0

只有一個記錄在圖片表和ImageUrl列有〜/圖片/個人資料/姨媽Yolanda.jpg和picdefault列是:真 –

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);