参考了别人的一个例子,向Sql数据库中插入图片时,出现:“未将对象引用设置到对象的实例”,错误在fileLength = Up_file.PostedFile.ContentLength;这句,调试时还出现在 System.NullReferenceException 中第一次偶然出现的“App_Web_5y6xpzfn.dll”类型的异常的说明,以下是原代码:
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UploadImage : System.Web.UI.Page
{
protected HtmlInputFile Up_file;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int fileLength ;
Up_file = new HtmlInputFile();
fileLength = Up_file.PostedFile.ContentLength;
try
{
if (fileLength == 0)
{
LB_message.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[fileLength];
Stream StreamObject = Up_file.PostedFile.InputStream;
StreamObject.Read(FileByteArray, 0, fileLength);
SqlConnection Con = new SqlConnection("Data Source=(local);Initial Catalog=xiaoyuan;uid=sa;pwd=sa;");
string SqlCmd = "insert into z_UserInfo(User_image,User_imageType)values(@User_image,@User_imageType)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@User_image", SqlDbType.Binary, fileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@User_imageType", SqlDbType.VarChar, 50).Value = Up_file.PostedFile.ContentType;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
LB_message.Text = "<p><b>OK!你已经成功上传你的图片</b>"; }
}
catch (Exception ex)
{
LB_message.Text = ex.Message.ToString();
} }
}
小弟是初学者,这是我的实习作业,周末就要交了,恳求大哥们赐教,不盛感激。
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class UploadImage : System.Web.UI.Page
{
protected HtmlInputFile Up_file;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int fileLength ;
Up_file = new HtmlInputFile();
fileLength = Up_file.PostedFile.ContentLength;
try
{
if (fileLength == 0)
{
LB_message.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[fileLength];
Stream StreamObject = Up_file.PostedFile.InputStream;
StreamObject.Read(FileByteArray, 0, fileLength);
SqlConnection Con = new SqlConnection("Data Source=(local);Initial Catalog=xiaoyuan;uid=sa;pwd=sa;");
string SqlCmd = "insert into z_UserInfo(User_image,User_imageType)values(@User_image,@User_imageType)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@User_image", SqlDbType.Binary, fileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@User_imageType", SqlDbType.VarChar, 50).Value = Up_file.PostedFile.ContentType;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
LB_message.Text = "<p><b>OK!你已经成功上传你的图片</b>"; }
}
catch (Exception ex)
{
LB_message.Text = ex.Message.ToString();
} }
}
小弟是初学者,这是我的实习作业,周末就要交了,恳求大哥们赐教,不盛感激。
解决方案 »
- 关于值类型和引用类型
- 又卡壳了,给ComboBox.SelectedValue付值
- 在.net中如何实现类似office project中实现的那种进度图
- 我想问网络通信方面的问题
- vc#2005ExpressBeta2中没有ClassDesigner?
- winfrom怎么创建共享文件夹
- 题外话:你需要什么样的项目负责人带你
- VS2005 创建的pda程序读取xml的问题!!!。着急!谢谢高手帮我解决!!!
- 迭代器yield问题,有高手可以帮解决下吗?
- visual studio 2015 打开项目时卡住卡在“准备项目”
- 数据库占内存越来越大怎么办?
- C#中如何获取通过Thread类调用的Excel信息
CmdObj .Parameters.Add("@img", System.Data.SqlDbType.Image);
CmdObj .Parameters["@img"].Value = FileByteArray;
fileLength = Up_file.PostedFile.ContentLength;Upfile是刚刚创建的,没有Up_file.PostedFile实例哦,当然会报错了。上载的要点:1.<form id="form1" enctype="multipart/form-data" method="post" runat="server" style="height:900px">2. HttpFileCollection files = Request.Files;
Up_file = new HtmlInputFile();//什么意思?
因为调试的时候老是说我未将对象实例化,所以加上了Up_file = new HtmlInputFile();,小弟初学者,不知怎么办,请大哥不吝赐教。to:xxoo2007()
大哥,能否再说的详细点,我是初学者,特别是第2条的具体用法,不胜感激
<input type="File" name="aa"/>
</form>这个是前台的,注意enctype属性还有name="aa"都是必须的。后台:
HttpFileCollection files = Request.Files; //reuquest对象是请求对象,里面带有上载文件的信息
for (int iFile = 0; iFile < files.Count; iFile++) { //循环处理多个上载的文件
HttpPostedFile postedFile = files[iFile];
string fileName;
string saveName;
fileName = System.IO.Path.GetFileName(postedFile.FileName); //取得上载文件名称
if (fileName != "" && postedFile.ContentLength / 1024 <= 5000)
{
....... //处理一下,生成文件名称等等 postedFile.SaveAs(saveName); //实际存盘,savename使用服务器的路径地址
}
--------------------------------
Up_file.PostedFile=null
当然会报错检查你的控件是否已经有值
控件是没值,怎么给控件即HtmlInputFile赋值呢?或者得到控件的值
试了您的方法,但是说HttpPostedFile postedFile = files[iFile];此句索引值超出范围,不知是怎么回事。