2013-05-17 42 views
0

我試圖使用ajaxfileupload上傳圖像(使用二進制)到我的數據庫(sql server 2008)。我不確定我是否正確執行了後面的代碼。當我嘗試發送報告沒有或與上傳任何圖片時出現一些錯誤彈出窗口。我發佈了下面的錯誤。請幫助我看看出了什麼問題。 謝謝。無法通過使用ajaxfileupload二進制插入圖像

AJAXFileUpload

<asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
    ThrobberID="myThrobber" 
    ContextKeys="fred" 
    AllowedFileTypes="jpg" 
    MaximumNumberOfFiles=1 
    UploadedComplete="AjaxFileUpload1_UploadedComplete" 
    /> 

背後代碼

using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace MP 
{ 
public partial class Report : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     String fullname = (String)Session["fullname"]; 
     String contact = (String)Session["contact"]; 

     lblFullName.Text = fullname; 
     lblContact.Text = contact; 
     lblDateTime.Text = DateTime.Now.ToString(); 

     Session["datetime"] = lblDateTime.Text; 
    } 

    protected void btnReport_Click(object sender, EventArgs e) 
    { 
     String fullname = (String)Session["fullname"]; 
     String contact = (String)Session["contact"]; 
     String datetime = (String)Session["datetime"]; 
     String typeofcrime = ddlTOC.SelectedItem.Text; 
     String location = txtLocation.Text; 
     String detail = txtDetail.Text; 
     String picture = (String)Session["picture"]; 

     if (picture.Equals("")) 
     { 
      SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True"); 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand("Insert into Report (fullname, contact, typeofcrime, location, CRdatetime, citizenreport) values ('" + fullname + "','" + contact + "','" + typeofcrime + "','" + location.Trim() + "','" + datetime + "','" + detail.Trim() + "')", conn); 
      cmd.ExecuteNonQuery(); 

      lblMessage.Text = "Report Submitted."; 

      conn.Close(); 

      txtDetail.Text = ""; 
      txtLocation.Text = ""; 
     } 
     else if (!picture.Equals("")) 
     { 
      SqlConnection conn = new SqlConnection("Data Source=localhost; Initial Catalog=project; Integrated Security=True"); 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand("Insert into Report (fullname, contact, typeofcrime, location, CRdatetime, citizenreport, picture) values ('" + fullname + "','" + contact + "','" + typeofcrime + "','" + location.Trim() + "','" + datetime + "','" + detail.Trim() + "','" + picture + "')", conn); 
      cmd.ExecuteNonQuery(); 

      lblMessage.Text = "Report Submitted."; 

      conn.Close(); 

      txtDetail.Text = ""; 
      txtLocation.Text = ""; 
     } 
    } 

    protected void btnCancel_Click(object sender, EventArgs e) 
    { 
     txtDetail.Text = ""; 
     txtLocation.Text = ""; 
    } 
    protected void AjaxFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e) 
    { 
     byte[] image = e.GetContents(); 
     Session["picture"] = image; 
    } 
} 
} 

錯誤(當而不上傳圖片報告)

Pic

錯誤(當i按上傳)

pic

+0

爲什麼要轉換會話[ 「圖片報」]爲String。 –

+0

如何在不使用會話存儲它的情況下以其他方法獲取二進制文件? – XiAnG

回答

0

嘗試if (string.IsNullOrEmpty(picture)代替if (picture.Equals("")); Objetc.equals是比較對象,請參見Msdn

+0

謝謝,它沒有上傳圖像時工作。任何想法如何使上傳圖像部分工作? – XiAnG

+0

你爲什麼要將Session [「picture」]轉換爲String。在btnReport_Click事件中。在AjaxFileUpload1_UploadComplete事件上。將會話設置爲空,如果沒有任何文件,則將字節分配給會話變量。並將byte []數組傳遞給查詢以將圖像保存到數據庫中。還有一個建議,如果你只是在單頁上使用這個圖像,要很好的使用viewstate來保存圖像。 –

+0

我仍然在嘗試瞭解它,所以我不確定會話是否能夠存儲字節。 – XiAnG