图片上传出错!急!!!!

yuan0211 2003-11-13 12:05:52

我是在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>");
}
}


}
}

...全文
91 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
comy 2003-11-13
  • 打赏
  • 举报
回复
do you added encType="multipart/form-data" like this?
<form id="Default" method="post" encType="multipart/form-data" runat="server">
yuan0211 2003-11-13
  • 打赏
  • 举报
回复
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<%@ Page language="c#" Codebehind="default.aspx.cs" Inherits="MobileWebApplication17.MobileWebForm1" AutoEventWireup="false" %>
<HEAD>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="C#">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/Mobile/Page">
</HEAD>
<body Xmlns:mobile="http://schemas.microsoft.com/Mobile/WebForm">
<mobile:Form id="Form1" enctype="multipart/form-data" runat="server">
<FONT face="宋体">
<mobile:Label id="Label1" runat="server" Alignment="Center" Font-Name="Arial">图片上传系统</mobile:Label>
<mobile:Label id="Label2" runat="server">请输入要上传编号:</mobile:Label>
<mobile:TextBox id="TextBox1" runat="server"></mobile:TextBox>
<mobile:Label id="Label3" runat="server">请选择要上传的图片:</mobile:Label>
<mobile:Command id="Command1" runat="server">上传</mobile:Command>  </FONT>
<mobile:Panel id="Panel2" runat="server">
<asp:Panel id="Panel1" runat="server">Panel<BR>
<BR>
<INPUT id="up_file" type="file" name="up_file" runat="server">
<BR></asp:Panel>
</mobile:Panel>
</mobile:Form>
</body>

去掉enctype="multipart/form-data",还是错的,,
我现在用的是移动控件,在做移动asp.net的项目,遇到麻烦了,希望大家可以帮我,谢谢!

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧