上一篇是用基本的form方法上传文件。
这一篇加入了用ajax来上传文件
控制器代码
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/");