FileUpload1.PostedFile.FileName 未将对象引用设置到对象的实例 我使用的是<asp:FileUpload ID="FileUpload" runat="server" Width="400px" />处理的代码是:String fn = this.FileUpload.PostedFile.FileName;报的错误是:{"未将对象引用设置到对象的实例。"} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String fn = this.FileUpload.PostedFile.FileName; 1L,2L,我对你无语……大小写都可以的……至于LZ的问题……要么是你没选择文件就提交了……要么就是页面刷新你选择的文件丢失了……代码再贴多一点…… 顶3楼,还有可能放到UpdatePanel中了啊总之楼主得描述清楚,完全的情况 是不是没有选择文件就点提交了 或者放到ajax的UpdatePanel里面了最好在提交之前先判断一下是否为空,给出提示 String是一个类,string是一种数据类型. string是c#中的类,String是.net Framework的类(在c# IDE中不会显示蓝色) c# string映射为.net Framework的String 如果用string,编译器会把它编译成String,所以如果直接用String就可以让编译器少做一点点工作 如果使用c#,建议使用string,比较符合规范 string始终代表 System.String(1.x) 或 ::System.String(2.0) ,String只有在前面有using System;的时候并且当前命名空间中没有名为String的类型(class、struct、delegate、enum)的时候才代表System.String string是关键字,String不是,也就是说string不能作为类、结构、枚举、字段、变量、方法、属性的名称,而String可以 string fullname = FileUpload1.FileName.ToString();这个是我用的。 对,是放在UpdatePanel中的 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertImage_WinForm.aspx.cs" Inherits="hospital.Tools.InsertImage_WinForm" %><!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> <script type="text/javascript"> function pageLoad() { } function closeWin(){ close(); } </script> <style type="text/css"> .style1 { width: 100%; } .style2 { width: 197px; } .style4 { width: 69px; } .style5 { width: 51px; } #File1 { width: 400px; } .style6 { width: 338px; } </style></head><body> <form id="form1" runat="server" enctype="multipart/form-data"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <table cellpadding="2" class="style1"> <tr> <td> <fieldset> <legend >图片管理</legend> <table cellspacing="1" class="style1"> <tr> <td class="style2"> <asp:Panel ID="Panel1" runat="server" Height="300px" Width="300px"> <table cellpadding="0" cellspacing="0" class="style1"> <tr> <td> <asp:Label ID="Label2" runat="server" Text="站内图片"></asp:Label> </td> </tr> <tr> <td> <asp:TreeView ID="TreeViewImg" runat="server" onselectednodechanged="TreeViewImg_SelectedNodeChanged" ShowLines="True"> </asp:TreeView> </td> </tr> </table> </asp:Panel> </td> <td> <asp:Panel ID="Panel2" runat="server" Height="300px" Width="300px"> <asp:Image ID="Image" runat="server" Height="300px" /> </asp:Panel> </td> </tr> </table> </fieldset> </td> </tr> <tr> <td> <fieldset> <legend >图片上传</legend> <table> <tr> <td> <table class="style1"> <tr> <td class="style4"> <asp:Label ID="Label1" runat="server" Text="选择图片" Width="80px"></asp:Label> </td> <td class="style6"> <asp:FileUpload ID="FileUpload" runat="server" Width="400px" /> </td> <td class="style5"> <asp:Button ID="btnUpfile" runat="server" Text="上传" onclick="btnUpfile_Click" Width="60px" /> </td> <td> <asp:Button ID="btnOK" runat="server" Text="确定" onclientclick="closeWin()" Width="60px" /> </td> </tr> </table> </td> </tr> <tr> <td> <asp:Label ID="lblInfo" runat="server"></asp:Label> </td> </tr> </table> </fieldset> </td> </tr> </table> </ContentTemplate> </asp:UpdatePanel> </div> </form></body></html>cs代码 protected void btnUpfile_Click(object sender, EventArgs e) { String[] types = ConfigurationManager.AppSettings["imgType"].Split('|'); int size = int.Parse(ConfigurationManager.AppSettings["imgSize"]); if (FileUpload.HasFile) { String fileName = this.FileUpload.PostedFile.FileName; int fileSize = this.FileUpload.PostedFile.ContentLength; lblInfo.Text = FileUpload.FileName; if (fileSize < size) { lblInfo.Text = "图片大小不能超过1MB"; return; } else { Response.Write(fileSize.ToString()); } } else { lblInfo.Text = "未选择任何文件!"; } 呵呵,Google上查FileUpload UpdatePanel,一堆答案 这个是要作为弹出的模式对话框用的,所以所有的按钮都要放进UpdatePanel里,不然在使用的时候会弹出新页面。有没有别的方法获得那个文件地址。谢谢!! 证书验证,服务器提交了协议冲突. Section=ResponseStatusLine 字符串問題 关于远程获取网页内容,asp.net错误,asp却正常的问题 怎样让文字靠底显示 网站合并问题! 求救!!!! 求助:一规则表达式的写法 和 [推荐书籍] 怎么调出opendialog对话框 第一个ASP.NET还没创建出来就遇到问题了,HELP MVC绑定多个实体时,验证信息为什么不显示? 显示图片问题 做过城市无刷新联动的朋友, 可进来看看。
总之楼主得描述清楚,完全的情况
或者放到ajax的UpdatePanel里面了最好在提交之前先判断一下是否为空,给出提示
string是c#中的类,String是.net Framework的类(在c# IDE中不会显示蓝色)
c# string映射为.net Framework的String
如果用string,编译器会把它编译成String,所以如果直接用String就可以让编译器少做一点点工作
如果使用c#,建议使用string,比较符合规范
string始终代表 System.String(1.x) 或 ::System.String(2.0) ,String只有在前面有using System;的时候并且当前命名空间中没有名为String的类型(class、struct、delegate、enum)的时候才代表System.String
string是关键字,String不是,也就是说string不能作为类、结构、枚举、字段、变量、方法、属性的名称,而String可以
这个是我用的。
对,是放在UpdatePanel中的
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertImage_WinForm.aspx.cs" Inherits="hospital.Tools.InsertImage_WinForm" %><!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>
<script type="text/javascript">
function pageLoad() {
}
function closeWin(){
close();
} </script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 197px;
}
.style4
{
width: 69px;
}
.style5
{
width: 51px;
}
#File1
{
width: 400px;
}
.style6
{
width: 338px;
}
</style>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table cellpadding="2" class="style1">
<tr>
<td>
<fieldset>
<legend >图片管理</legend>
<table cellspacing="1" class="style1">
<tr>
<td class="style2">
<asp:Panel ID="Panel1" runat="server" Height="300px" Width="300px">
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="站内图片"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TreeView ID="TreeViewImg" runat="server"
onselectednodechanged="TreeViewImg_SelectedNodeChanged" ShowLines="True">
</asp:TreeView>
</td>
</tr>
</table>
</asp:Panel>
</td>
<td>
<asp:Panel ID="Panel2" runat="server" Height="300px" Width="300px">
<asp:Image ID="Image" runat="server" Height="300px" />
</asp:Panel>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
<tr>
<td>
<fieldset>
<legend >图片上传</legend>
<table>
<tr>
<td>
<table class="style1">
<tr>
<td class="style4">
<asp:Label ID="Label1" runat="server" Text="选择图片" Width="80px"></asp:Label>
</td>
<td class="style6">
<asp:FileUpload ID="FileUpload" runat="server" Width="400px" />
</td>
<td class="style5">
<asp:Button ID="btnUpfile" runat="server" Text="上传" onclick="btnUpfile_Click"
Width="60px" />
</td>
<td>
<asp:Button ID="btnOK" runat="server" Text="确定" onclientclick="closeWin()"
Width="60px" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblInfo" runat="server"></asp:Label>
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
cs代码 protected void btnUpfile_Click(object sender, EventArgs e)
{
String[] types = ConfigurationManager.AppSettings["imgType"].Split('|');
int size = int.Parse(ConfigurationManager.AppSettings["imgSize"]); if (FileUpload.HasFile)
{
String fileName = this.FileUpload.PostedFile.FileName;
int fileSize = this.FileUpload.PostedFile.ContentLength;
lblInfo.Text = FileUpload.FileName;
if (fileSize < size)
{
lblInfo.Text = "图片大小不能超过1MB";
return;
}
else
{
Response.Write(fileSize.ToString()); }
}
else {
lblInfo.Text = "未选择任何文件!";
}