首页 > 其他分享 >.net 下载和上传文件

.net 下载和上传文件

时间:2022-12-08 15:12:51浏览次数:43  
标签:CUT string FileName IsNullOrWhiteSpace new net 上传 ID 下载

上传

 public BesaBO Upload(string CUT_ID,string Code) 
        {
            if (string.IsNullOrWhiteSpace(CUT_ID) || string.IsNullOrWhiteSpace(Code))
            {
                return new BesaBO() { code = 20001, message = "参数不对!" };
            }
            HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["file"];
            //获取上载文件名称
            string _FileName = file.FileName;
            //后缀
            string _Ext = _FileName.Substring(_FileName.LastIndexOf("."));
            //新文件名
            string _NewName = Guid.NewGuid().ToString();
            //获取上传路径
            string ConfigPath = ConfigurationManager.AppSettings["UploadFilePath"].ToString();

            System.IO.Directory.CreateDirectory(ConfigPath);
            var paths = System.IO.Path.Combine(ConfigPath, _NewName + _Ext);
            file.SaveAs(paths);
            var db = DBClient.GetInstance();

            db.Insertable<Fileinfo>(new Fileinfo() {ID = _NewName,Code = Code,Suffix = _Ext,CREATED_TIME = DateTime.Now,CUT_ID = CUT_ID,Name = _FileName,UPDATED_TIME = DateTime.Now }).ExecuteCommand();
            return new BesaBO();
        }

  下载

     public FileStreamResult download(string Url,string Name)
        {
            if (string.IsNullOrWhiteSpace(Url) || string.IsNullOrWhiteSpace(Name)) throw new Exception("文件名为空!");
            //string name = url.Substring(url.LastIndexOf(@"\") + 1).Split(".".ToArray())[0];
            var stream = new FileStream(Url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return File(stream, "application/octet-stream", Name);
        }

  

标签:CUT,string,FileName,IsNullOrWhiteSpace,new,net,上传,ID,下载
From: https://www.cnblogs.com/gavin-gu/p/16966124.html

相关文章