我参考别人的做法,自己写了一个,可是说“未将对象实例化”,我看了半天也没看出来问题在哪。请大哥们指教。
protected void Button1_Click(object sender, EventArgs e)
{
int intImageSize;
string strImageType;
Stream ImageStream;
intImageSize = personImage.PostedFile.ContentLength ; // 文件大小
strImageType = personImage.PostedFile.ContentType; // 文件类型
ImageStream = personImage.PostedFile.InputStream;
byte[] ImageContent = new byte[intImageSize];
int intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
// 写入数据库
string strConn = "Data Source=(local);initial catalog=xiaoyuan;uid=sa;pwd=sa";
SqlConnection myConnection = new SqlConnection(strConn);
SqlCommand myCommand = new SqlCommand("z_UserInfo", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@User_image", SqlDbType.Image).Value = ImageContent;
myCommand.Parameters.Add("@User_imageType", SqlDbType.VarChar, 255).Value = strImageType;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("添加成功!");
}
catch(System.Exception SQLExe)
{
Response.Write("添加失败!原因:"+SQLExe.ToString());
}
}
出错的是intImageSize = personImage.PostedFile.ContentLength ; ,说“未将对象实例化”“,求大哥们帮看看,小弟不盛感激
protected void Button1_Click(object sender, EventArgs e)
{
int intImageSize;
string strImageType;
Stream ImageStream;
intImageSize = personImage.PostedFile.ContentLength ; // 文件大小
strImageType = personImage.PostedFile.ContentType; // 文件类型
ImageStream = personImage.PostedFile.InputStream;
byte[] ImageContent = new byte[intImageSize];
int intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
// 写入数据库
string strConn = "Data Source=(local);initial catalog=xiaoyuan;uid=sa;pwd=sa";
SqlConnection myConnection = new SqlConnection(strConn);
SqlCommand myCommand = new SqlCommand("z_UserInfo", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@User_image", SqlDbType.Image).Value = ImageContent;
myCommand.Parameters.Add("@User_imageType", SqlDbType.VarChar, 255).Value = strImageType;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("添加成功!");
}
catch(System.Exception SQLExe)
{
Response.Write("添加失败!原因:"+SQLExe.ToString());
}
}
出错的是intImageSize = personImage.PostedFile.ContentLength ; ,说“未将对象实例化”“,求大哥们帮看看,小弟不盛感激
解决方案 »
- 16进制 byte 转 16进制 byte
- 请看下面的代码:
- 求WPF快速学习书籍或方法
- WinForm控件的TabControl如何让每个选项都显示在下方,类似于Excel下面的Sheet1
- 求救各位大侠!cmpp动态链接库的问题
- 解决马上给分!!做Excel的一个插件,如何制作它的安装程序?(要考虑支持Excel的不同版本问题)
- 你可以不答~~~真的
- 请教,C#程序能否编写类似于金山词霸的软件,能否控制其它应用程序,向其他的应用程序中发送控制消息,能否监视用户的键盘和鼠标操作。
- 这几个命名空间我要到哪里去找
- C#我在做一个停车系统
- 想转行学C#,了解一下行情,看看代遇如何!谢谢!
- 求一个c#操作mysql数据库
HGRD.DBHelper.SqlDataParam sp = new HGRD.DBHelper.SqlDataParam();
sp.Add("@TName", this.TextBox1.Text);
sp.Add("@TSex", this.TextBox2.Text == "男" ? "1" : "0");
sp.Add("@TWork", this.TextBox3.Text);
sp.Add("@TMemo", this.TextBox4.Text);
int len = FileUpload1.PostedFile.ContentLength;
byte[] pic = new byte[len];
this.FileUpload1.PostedFile.InputStream.Read(pic, 0, len);
sp.Add("@TIMAGE", pic);
int i = HGRD.DBHelper.WebDBHelper.ExecuteNonQuery(sql, sp);
if (i == 0)
Response.Write("添加失败!");
else
Response.Write("添加成功!");
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg"; context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.BufferOutput = false; if (context.Request.QueryString["guid"] == null) return; Stream stream = GetPhoto(context.Request.QueryString.Get("guid"));//==★Get the photo stream const int buffersize = 1024 * 16; byte[] buffer = new byte[buffersize]; int count = stream.Read(buffer, 0, buffersize);//==★the from position is always 0? while (count > 0)
{ context.Response.OutputStream.Write(buffer, 0, count); count = stream.Read(buffer, 0, buffersize); } }
public bool IsReusable {
get {
return false;
}
}
public Stream GetPhoto(string guid)
{ string sql = "select TImage from TestTable where TGUID='{0}'";
sql = string.Format(sql, guid);
object result = HGRD.DBHelper.WebDBHelper.ExecuteScalar(sql);//==★return the first Column of the first Row
try
{ //==return new MemoryStream(CType(result,byte())) return new MemoryStream((byte[])result);//==★if use vb,how to replace this word. } catch (ArgumentNullException e)
{ return null; }
}}
HttpPostedFile UpFile = Up_file.PostedFile;这行代码using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
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 Int32 fileLength = 0;
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile UpFile = Up_file.PostedFile;
fileLength = UpFile.ContentLength;
try
{
if (fileLength == 0)
{
LB_message.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[fileLength];
Stream StreamObject = UpFile.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 = UpFile.ContentType;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
LB_message.Text = "<p><b>OK!你已经成功上传你的图片</b>"; }
}
catch (Exception ex)
{
LB_message.Text = ex.Message.ToString();
} }
}
我想做的是B/S结构的,您的代码是C/S结构的,不过仍然非常感谢
可是还是说“未将对象实例化”,错在
HttpPostedFile UpFile = Up_file.PostedFile;这行代码 正解:
<INPUT id="Up_file" type="file" size="52" runat="server">记得要写上:runat="server"