.net MVC 文件上传

31 篇文章 0 订阅
17 篇文章 0 订阅


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
 
namespace UploadFileTest.Controllers
{
    public class UploadTestController : Controller
    {
        // GET: UploadTest
        public ActionResult Index()
        {
            return View();
        }
 
 
        //这个view是用来选择上传文件的
        public ActionResult Upload()
        {
            return View();
 
        }
 
        //接收ajax提交的数据,并保存文件到服务器上
        [HttpPost]
        public string AjaxSaveAs(HttpPostedFileBase MyFile)
        {
            //得到的名字是文件在本地机器的绝对路径
            var strLocalFullPathName = MyFile.FileName;
            //提取出单独的文件名,不需要路径
            var strFileName = Path.GetFileName(strLocalFullPathName);
            //定义服务器的文件夹,用来保存文件
            var strServerFilePath = Server.MapPath("/docs/");
            //将接收到文件保存在服务器指定上当
            MyFile.SaveAs(Path.Combine(strServerFilePath, strFileName));
 
            return "upload done from server!";
 
        }
 
        //接收form 提交的数据,这个action是用来接收文件并保存在服务器上
        [HttpPost]
       public ActionResult SaveAs(HttpPostedFileBase MyFile)
        {
            //得到的名字是文件在本地机器的绝对路径
            var strLocalFullPathName = MyFile.FileName;
            //提取出单独的文件名,不需要路径
            var strFileName = Path.GetFileName(strLocalFullPathName);
            //定义服务器的文件夹,用来保存文件
            var strServerFilePath = Server.MapPath("/docs/");
            //将接收到文件保存在服务器指定上当
            MyFile.SaveAs(Path.Combine(strServerFilePath,strFileName));
 
            //下面只是用来显示一些相关字符串做测试用
            ViewBag.strLocalFullPathName = strLocalFullPathName;
            ViewBag.strFileName = strFileName;
            ViewBag.strServerFilePath = strServerFilePath;
 
            return View();
 
        }
 
        
    }
}
Upload.cshtml里的代码

@{
    ViewBag.Title = "Upload";
}
 
<h2>Upload</h2>
 
<script>
    function UploadFile() {
        var currentFile = document.getElementById("myFileAjax").files;
        var file = currentFile[0];
        var formData = new FormData();
        formData.append("MyFile", file)
        var xhr = new XMLHttpRequest();
 
        //xhr.onload = function () {
        //    alert(xhr.responseText);
 
        //}
 
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert(xhr.responseText);
            }
        }
 
        xhr.open("post", "/UploadTest/AjaxSaveAs", true)
        xhr.send(formData);
        document.getElementById("divMsg").innerHTML = "good";
    }
 
 
</script>
 
 
@*利用ajax进行文件上传*@
<div>
    <input type="file" name="myFileAjax" id="myFileAjax" />
    <input type="button" οnclick="UploadFile();" value="UploadAjax" />
</div>
<div id="divMsg"></div>
 
 
 
@*利用基本的form提交,进行文件上传*@
<form action="/UploadTest/SaveAs" method="post" enctype="multipart/form-data">
    <div>
 
        <input type="file" id="MyFile" name="MyFile" />
        <input type="submit" value="Upload" />
    </div>
</form>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值