.Net 上传文件和下载文件

时间:2024-10-10 20:02:56

一、上传文件

  1、普通的form表单提交

  注意点:

  1. 请求方式必须为Post。
  2. form表单里必须设置enctype属性(enctype = "multipart/form-data")

下面的代码是实现Demo:  

<div class="row">
@using(Html.BeginForm("Export", "Home", FormMethod.Post, new { @class = "form-horzoontal", @enctype = "multipart/form-data" }))
{
<div class="form-group">
<label class="label-control">请选择文件</label>
<div class="col-md-4">
@Html.TextBox("File","", new { @type="file",@class="form-ontrol"})
</div>
</div>
<div class="form-group">
<div class="clearfix">
<button type="submit" class="btn btn-primary">
<i class="icon-ok"></i>提交
</button>
<button type="reset" class="btn btn-danger">
<i class="icon-undo"></i>重置
</button>
</div>
</div>
}
</div>
/// <summary>
/// 提交文件
/// </summary>
[HttpPost]
public void Export(HttpPostedFileBase File)
{
//HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//HttpPostedFile file = files[0];
//拓展名
string Extension = System.IO.Path.GetExtension(File.FileName);
//文件名
string FileName = DateTime.Now.ToFileTime() + Extension;
//文件存放路径
string Path = Server.MapPath("~/upload/");
//如果文件路径不存在
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
} File.SaveAs(Path + "//" + FileName);
}

  2、Ajax表单提交 

  注意点:

  1. 导入jquery.unobtrusive-ajax.min.jsjquery.form.js两个引用。
  2. 提交的时候必须用 $("#form").ajaxSubmit()的提交方式。否则接收不到文件流。 

  下面的代码是实现Demo:

<div class="row">
@using(Ajax.BeginForm("Export", "Home", null,new AjaxOptions { HttpMethod="Post"}, new {@id="form", @class = "form-horzoontal", @enctype = "multipart/form-data" }))
{
<div class="form-group">
<label class="label-control">请选择文件</label>
<div class="col-md-4">
@Html.TextBox("File","", new { @type="file",@class="form-ontrol"})
</div>
</div>
<div class="form-group">
<div class="clearfix">
<button type="button" class="btn btn-primary" onclick="sub()">
<i class="icon-ok"></i>提交
</button>
<button type="reset" class="btn btn-danger">
<i class="icon-undo"></i>重置
</button>
</div>
</div>
}
</div>
@section Scripts{
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.form.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
//ajax提交表单
function sub() {
$("#form").ajaxSubmit();
}
</script>
}
/// <summary>
/// 提交文件
/// </summary>
[HttpPost]
public void Export(HttpPostedFileBase File)
{
//HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//HttpPostedFile file = files[0];
//拓展名
string Extension = System.IO.Path.GetExtension(File.FileName);
//文件名
string FileName = DateTime.Now.ToFileTime() + Extension;
//文件存放路径
string Path = Server.MapPath("~/upload/");
//如果文件路径不存在
if (!System.IO.Directory.Exists(Path))
{
System.IO.Directory.CreateDirectory(Path);
} File.SaveAs(Path + "//" + FileName);
}

二、下载文件

  下面是两种实现方式

  1、

/// <summary>
/// 下载文件
/// </summary>
public void DownloadFile()
{
//获取respone对象
HttpResponse respone = System.Web.HttpContext.Current.Response;
//文件绝对路径
string Path = Server.MapPath("~/upload/131348099436645924.JPG");
string fileName = System.IO.Path.GetFileName(Path);
System.IO.FileInfo file = new FileInfo(Path);
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
//响应类型
Response.ContentType = "application/octet-stream";
Response.WriteFile(Path);
Response.Flush();
Response.End();
}

  2、

/// <summary>
/// 下载文件
/// </summary>
public FileResult DownloadFile()
{
//文件绝对路径
string Path = Server.MapPath("~/upload/131348099436645924.JPG");
string fileName = System.IO.Path.GetFileName(Path);
return File(Path, "application/octet-stream",fileName);
}