图片上传出错!急!!!!
我是在html里面取了个file field,把它放到一个Panel上面,如果不加Panel,会出错,但是现在错误提示:
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 66:
行 67: HttpPostedFile upFile =up_file.PostedFile;
行 68: int iFileLength = upFile.ContentLength;
行 69: if(iFileLength == 0)
行 70: {
源文件: f:\公司项目\mobilewebapplication17\default.aspx.cs 行: 68
堆栈跟踪:[NullReferenceException: 未将对象引用设置到对象的实例。]
MobileWebApplication17.MobileWebForm1.Command1_Click(Object sender, EventArgs e) in f:\公司项目\mobilewebapplication17\default.aspx.cs:68
System.Web.UI.MobileControls.Command.OnClick(EventArgs e)
System.Web.UI.MobileControls.Command.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String argument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.MobileControls.MobilePage.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
能否帮我看一下错误!谢谢你,感激不尽!谢谢
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
namespace MobileWebApplication17
{
/// <summary>
/// MobileWebForm1 的摘要说明。
/// </summary>
public class MobileWebForm1 : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Label Label1;
protected System.Web.UI.MobileControls.Label Label2;
protected System.Web.UI.MobileControls.TextBox TextBox1;
protected System.Web.UI.MobileControls.Label Label3;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Panel Panel1;
protected System.Web.UI.HtmlControls.HtmlInputFile up_file;
protected System.Web.UI.MobileControls.Form Form1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Command1.Click += new System.EventHandler(this.Command1_Click);
this.Form1.Activate += new System.EventHandler(this.Form1_Activate);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Form1_Activate(object sender, System.EventArgs e)
{
}
private void Command1_Click(object sender, System.EventArgs e)
{
HttpPostedFile upFile =up_file.PostedFile;
int iFileLength = upFile.ContentLength;
if(iFileLength == 0)
{
Response.Write("<script>window.alert(\"请选择要上传的文件!\")</script>");
}
else
{
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=ACSC79WRMST01WZ;uid=sa;pwd=;database=police");
string sql = "insert into t_imgs (imgData) values "
+ "(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
//cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
//cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
//cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//txtDesc.Text = "";
Response.Write("<script>window.confirm(\"上传文件成功!\")</script>");
}
}
}
}