asp.net 保存文件到服务器上,asp.net中文件如何上传到服务器上

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

namespace SuAn

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

if (this.filepost.PostedFile.FileName == "")

{

Response.Write("上传文件不能为空!");

return;

}

try

{

string  Path = Server.MapPath("upload/");//设置服务器端路径

string filePath = this.filepost.PostedFile.FileName;//获取客户端实际路径

string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);//获取文件名称

string serverPath = Path + fileName;//上传的文件保存到服务器端的路径

System.Text.StringBuilder buider = new System.Text.StringBuilder();//上传的文件信息:可变字符串

buider.Append("上传文件的类型:"+this.filepost.PostedFile.ContentType.ToString()+"");

buider.Append("客户端文件地址:"+this.filepost.PostedFile.FileName+"");

buider.Append("上传文件名称:"+fileName);

buider.Append("上传文件的扩展名:"+filePath.Substring(fileName.LastIndexOf(".")+1));

buider.Append("上传文件的大小:"+this.filepost.PostedFile.ContentLength/1024+"k"+"");

if (System.IO.File.Exists(serverPath))

{

Response.Write("这个文件在服务器上已经存在,请不要重复上传!");

return;

}

string str = fileName.Substring(fileName.LastIndexOf(".")+1);//获取文件后缀名

if (str == "jpg" || str == "rar" || str == "ppt")

{

Response.Write("对不起,该类型文件不能被上传!");

return;

}

if (this.filepost.PostedFile.ContentLength >2048)

{

Response.Write("对不起,文件不能超过2K");

return;

}

this.filepost.PostedFile.SaveAs(serverPath);//上传保存文件

this.lblMessage.Text = buider.ToString();

}

catch(Exception ex)

{

Response.Write(ex.Message);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值