上传功能
css文件
<style>
<style >
body {
background-color: #EBEBEB
}
.aaa {
width: auto;
height: 600px;
background-color: #CB4F51;
padding-top: 100px;
padding: 50px;
display: block;
}
#box {
width: 800px;
height: 100px;
display: block;
margin: 0 auto;
text-align: center;
}
#file_name {
width: 400px;
height: 30px;
}
a.input {
width: 70px;
height: 30px;
line-height: 30px;
background: #3091d1;
text-align: center;
display: inline-block;
overflow: hidden;
position: relative;
top: 10px;
}
a.input:hover {
background: #31b0d5;
color: #ffffff;
}
a {
text-decoration: none;
color: #FFF;
}
#file {
opacity: 0;
filter: alpha(opacity=0);
font-size: 100px;
position: absolute;
top: 0;
right: 0;
}
#submit {
opacity: 0;
filter: alpha(opacity=0);
font-size: 100px;
position: absolute;
top: 0;
right: 0;
}
#t_box {
width: 100%;
margin-top: 400px;
margin-bottom: 20px;
text-align: center;
}
#t {
font-family: "微软雅黑";
color: darkblue;
}
</style>****
<h2>File</h2>
@using (Html.BeginForm("File", "Home", FormMethod.Post,new { enctype = "multipart/form-data" })) {
<div id="box">
<input type="text" id="file_name" name="file_name" readonly="readonly" value="未选择文件" />
<a href="javascript:void(0);" class="input">
浏览
<input type="file" id="file" name="document[file]">
</a>
<a href="javascript:void(0);" class="input">
上传
<input type="submit" name="sudmit" id="submit" value="上传" />
</a>
<br><br><br>
<div id="t_box">
<span id="t">值得信任的内容托管系统-8.49%的可靠性和累积2小时的无障碍运行性</span>
</div>
</div>
}
<script type="text/javascript">
$(function(){
$("#file").change(function(){
var fileSize = this.files[0].size;
var size = fileSize / 1024 / 1024;
if (size > 5) {
alert("附件不能大于5M,请将文件压缩后重新上传!");
this.value="";
return false;
} else {
var arr = $("#file").val().split('\\');
var fileName = arr[arr.length - 1];
$("#file_name").val(fileName);
}
});
$("form").submit(function(){
if($("#file").val()==""||$("#file").val()==null){
alert("未选择文件");
return false;
}
});
});
</script>
<p>Ajax.BeginForm</p>
@using(Ajax.BeginForm("SavePictureByForm", null, new AjaxOptions() { OnSuccess = "PostSuc", OnFailure = "PostFail", HttpMethod = "Post"}, new {enctype = "multipart/form-data"}))
{
<input type="file" name="pic" id="pic" multiple="multiple" />
<input type="submit" value="提交" />
}
后端处理上传请求
public ActionResult File()
{
HttpPostedFileBase file = Request.Files["document[file]"];
if (file != null) {
string fileName = Request.Form["file_name"];
string filePath = Path.Combine(HttpContext.Server.MapPath("../UpLoads/"+fileName));
file.SaveAs(filePath);
return RedirectToAction("File","Home");
}
ViewBag.Message = "Your contact page.";
return View();
}
下载功能
前端html
<h2>File</h2>
@using (Html.BeginForm("DownLoad", "Home", FormMethod.Get, new { enctype = "multipart/form-data" }))
{
<div id="box">
<input type="text" id="file_name" name="fileName" value="未选择文件" />
<a href="javascript:void(0);" class="input">
下载
<input type="submit" id="submit" value="下载" />
</a>
<br><br><br>
<div id="t_box">
<span id="t">值得信任的内容托管系统-8.49%的可靠性和累积2小时的无障碍运行性</span>
</div>
</div>
}
<script type="text/javascript">
$(function(){
$("form").submit(function(){
if($("#file_name").val()==""||$("#file_name").val()==null){
alert("输入你想获得文件");
return false;
}
});
});
</script>
后端处理下载请求
public ActionResult DownLoad(string fileName) {
if (fileName != null) {
string filePath = Path.Combine(HttpContext.Server.MapPath("../UpLoads/" + fileName));
return File(new FileStream(filePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}
return View();
}