我試圖使用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;
}
}
}
錯誤(當而不上傳圖片報告)
錯誤(當i按上傳)
爲什麼要轉換會話[ 「圖片報」]爲String。 –
如何在不使用會話存儲它的情況下以其他方法獲取二進制文件? – XiAnG