protected void UpLoadFile_Click(Object sender, EventArgs e)
{
string path = Server.MapPath("./") + "image/case/";//获取当前目录下upfile文件夹在服务器上的绝对地址,作为图片保存地址
if (UpLoadFile.HasFile)//判断本地需要上传的文件是否已经选取
{
string fileExt = System.IO.Path.GetExtension(UpLoadFile.FileName).ToLower();//获取上传文件的扩展名并转换为小写,为下面的上传类型合法性判断做准备
if (fileExt == ".gif" || fileExt == ".jpg")//只允许上传gif和jpg的文件格式
{
//开始执行上传操作
try
{
UpLoadFile.SaveAs(path + UpLoadFile.FileName);//服务端保存该文件
FileDetail.Visible = true;//显示上传文件的信息
//显示上传文件的名称,类型和大小
FileName.InnerHtml = UpLoadFile.PostedFile.FileName;
FileType.InnerHtml = FileName.InnerHtml.Substring(FileName.InnerHtml.LastIndexOf(".")).ToString().Trim();
FileSize.InnerHtml = (UpLoadFile.PostedFile.ContentLength / 1024) + "KB";
SaveURL.InnerHtml = path + UpLoadFile.FileName;//显示保存到服务器端文件的路径
Image1.Visible = true;
Image1.ImageUrl = "image/case/" + UpLoadFile.FileName;
string name = UpLoadFile.FileName.ToString();
Session["photo2"] = name;
}
catch (Exception ex)
{ Literal lt = new Literal();
lt.Text = "<script>alert('文件上传过程发生错误,错误信息: '" + ex.Message + "'')</script>";
this.Page.Controls.Add(lt); }
}
else
{ Literal li = new Literal();
li.Text = "<script>alert('程序只支持上传.gif或jpg格式的文件')</script>";
this.Page.Controls.Add(li);
}
}
else
{//如果没有选择本地文件
Literal l2 = new Literal();
l2.Text = "<script>alert('请先选择文件')</script>";
this.Page.Controls.Add(l2);
}
}
{
string path = Server.MapPath("./") + "image/case/";//获取当前目录下upfile文件夹在服务器上的绝对地址,作为图片保存地址
if (UpLoadFile.HasFile)//判断本地需要上传的文件是否已经选取
{
string fileExt = System.IO.Path.GetExtension(UpLoadFile.FileName).ToLower();//获取上传文件的扩展名并转换为小写,为下面的上传类型合法性判断做准备
if (fileExt == ".gif" || fileExt == ".jpg")//只允许上传gif和jpg的文件格式
{
//开始执行上传操作
try
{
UpLoadFile.SaveAs(path + UpLoadFile.FileName);//服务端保存该文件
FileDetail.Visible = true;//显示上传文件的信息
//显示上传文件的名称,类型和大小
FileName.InnerHtml = UpLoadFile.PostedFile.FileName;
FileType.InnerHtml = FileName.InnerHtml.Substring(FileName.InnerHtml.LastIndexOf(".")).ToString().Trim();
FileSize.InnerHtml = (UpLoadFile.PostedFile.ContentLength / 1024) + "KB";
SaveURL.InnerHtml = path + UpLoadFile.FileName;//显示保存到服务器端文件的路径
Image1.Visible = true;
Image1.ImageUrl = "image/case/" + UpLoadFile.FileName;
string name = UpLoadFile.FileName.ToString();
Session["photo2"] = name;
}
catch (Exception ex)
{ Literal lt = new Literal();
lt.Text = "<script>alert('文件上传过程发生错误,错误信息: '" + ex.Message + "'')</script>";
this.Page.Controls.Add(lt); }
}
else
{ Literal li = new Literal();
li.Text = "<script>alert('程序只支持上传.gif或jpg格式的文件')</script>";
this.Page.Controls.Add(li);
}
}
else
{//如果没有选择本地文件
Literal l2 = new Literal();
l2.Text = "<script>alert('请先选择文件')</script>";
this.Page.Controls.Add(l2);
}
}
解决方案 »
- 动态加载用户控件及传参
- asp.net 跳转页面刷新问题。在线等待!
- datagrid查到的记录为0的话就会什么也不显示了!我想这个时候让显示自已定制的列!怎样做呢?
- 大哥大姐帮帮忙,网站估价,回答有分!!
- 编辑状态下javascript的使用
- 有关VS2008和SQL 数据库文件相对路径的问题
- js怎么上传文件到WebService
- 怎么才能让Web中嵌入的MediaPlayer播放.asx节目单里的ref=http://.../*.nsc?
- 请帮我区分一下 用户控件、自定义类、模版 这三个配件的异同
- jeasy ui 的datagrid 中URL地址不能用。。/这样吗
- .html页面如何获取cookie?
- 跪求高手啊,我做了两个页面,一个有gridview,我想在点击gridview的一个超链接列时,把点击的这一行的数据传到下一个页面。跪求啊~~~~~
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
“string path = Server.MapPath("./") + "image/case/";//获取当前目录下upfile文件夹在服务器上的绝对地址,作为图片保存地址”
这其中的“Server.MapPath("./")”
Server.MapPath("~/upfile/image/case/")
等这样的绝对根路径
另外string path = Server.MapPath("./") + "image/case/"也是错误的,应该是
string path = Server.MapPath("./") + "image\\case\\" 这样的物理路径
说明有对象为null了。你需要进行检查
如果你上传个文件,内容为空,这个是false
<form id="Form1" runat="server" method="post" enctype="multipart/form-data"></form>
会不会有什么影响呢?