.net 下载和上传文件
上传
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();
}
下载 继承Controller
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);
}
by gjw
版权归作者所有!

浙公网安备 33010602011771号