System.NullReferenceException: Object reference not set to an instance of an object. 出错代码:string FilePath = PhotoUpload.PostedFile.FileName;为何!???????????
解决方案 »
- ASP.NET
- 求包含时间url正则
- 帮忙解决这个问题一定给高分
- 哪儿设置页面自动包含Content-Type
- datagrid列合并问题
- 使用VSS2005版本控制遇到的一点疑惑(和WINFORM有关)
- 存储session状态在SQL 服务器的数据库中Storing Session State in a SQL Server Database) 第一次文章贴出来,请指正,请鼓励
- 在c#如何生成显示最近12个月
- 分析器错误问题:未能加载类型。打不开网页,向高手求救!
- 请教哪里有asp.net的帮助文档或开发手册之类的,我是要用它查询Server控件的属性和方法?
- 装ajax 的问题
- 如何嵌套DataList
就是没有选择文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleFileUpLoad.aspx.cs"
Inherits="WebApplication1.SimpleFileUpLoad" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager> <script language="javascript" type="text/javascript">
function Clear(formId)
{
$clearHandlers($get(formId));
}
</script> <div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table style="width: 343px">
<tr>
<td style="width: 100px">
单文件上传
</td>
<td style="width: 100px">
</td>
</tr>
<tr>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<td style="width: 100px">
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" Width="478px" />
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td style="width: 100px">
<asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="上传" />
</td>
</ContentTemplate>
</asp:UpdatePanel>
</tr>
<tr>
<td style="width: 100px; height: 21px;">
<asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label>
</td>
<td style="width: 100px; height: 21px">
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Web.UI;namespace WebApplication1
{
public partial class SimpleFileUpLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bt_upload.Attributes.Add("onclick", "Clear('" + this.form1.ClientID + "');");
}
} protected void bt_upload_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName == "")
{
this.lb_info.Text = "请选择文件!";
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = Server.MapPath("images/") + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
this.lb_info.Text = "上传成功!";
}
}
catch (Exception ex)
{
this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
}
}
}
}