.net 用file上传文件到服务器方法

 后台

public  void Btn_click(){

   HttpFileCollection files = HttpContext.Current.Request.Files;
        HttpPostedFile postFile = files["Filetype"];
        string fileName = Path.GetFileName(postFile.FileName);
        string path = SaveFiles(fileName, "Filetype");

}

private string SaveFiles(string FileName,string iFile)

    {
        string imgPathPart = "";
        ///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;
        try
        {
            ///'检查文件扩展名字
            HttpPostedFile postedFile = files[iFile];
            string fileName;
            fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                //设置文件保存信息
                //设置文件保存路径
                string temPath = "diract" + @"\" + "" + FileName + "" + @"\" + DateTime.Now.Year + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
                string pathHead = ConfigurationManager.AppSettings["doc_path"];
                if (string.IsNullOrEmpty(pathHead))
                {
                    pathHead = @"D:\Uploads\MIS\";
                }
                string path = pathHead + temPath;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意:可能要修改你的文件夹的匿名写入权限。
                fileName = DateTime.Now.Ticks.ToString() + fileName;
                postedFile.SaveAs(path + fileName);
                imgPathPart = temPath + fileName;
            }
            else
            {
                imgPathPart = null;
            }
           
        }
        catch (System.Exception Ex)
        {


        }


        return imgPathPart;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值