Asp.net中用FileUpload控件上传文件

后台代码:
protected void Button1_Click(object sender, EventArgs e)
        {
string upTempFileBasePath = this.MapPath(@"~/Member/UserUpLoad/TempUpLoadFiles");
string uploaFileFormat = "|.jpg|.gif|";         //图片的格式也要在配置文件内appSettings节点上配置
string upFileName = Tools.CreateFileName();     //通过一个方法获得随机的文件名
string FileExtension = Path.GetExtension(UploadPhoto.FileName).ToString().Trim(); //取得上传文件的后缀名,带".",后缀名存在才有必要上传文件
string upSaveTempFilePath = upTempFileBasePath + "/" + upFileName + FileExtension;    //上传文件时的路径

if (uploaFileFormat.IndexOf(FileExtension.ToLower()) <= 0)
{
                            Tools.ShowMessage(this.Page, "请上传jpg或gif格式的图片文件!");
                            return;
}
if (UploadPhoto.PostedFile.ContentLength > 153600)
{
                            Tools.ShowMessage(this.Page, "请上传小于150K的图片文件!");   //上传文件的大小(字节)。
                            return;
}
UploadPhoto.SaveAs(upSaveTempFilePath);     //进行保存上传文件
}


前台代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>上传文件</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="UploadPhoto" runat="server" />
            <asp:Button ID="btnUpLoadFile" runat="server" Text="确定上传" οnclick="btnUpLoadFile_Click" />
        </div>
    </form>
</body>
</html>

要引用: using System.IO


此方法可以判断文件的上传类型和文件大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值