我使用VS2015创建了一个WebApi项目。具体来说,我试图编写一个简单的Web服务来使用POST上传文件。
我在使用以前版本的Visual Studio之前已经这样做了,但是VS2015使用的是ASP.NET 5和MVC 6,它显然有一些主要的api更改。
之前,我可以使用这样的东西:
Request.Content.ReadAsMultipartAsync().Result.Contents
顺便说一下,这就是我用Google搜索的每个网站推荐的内容。但是我的VS2015副本给了我以下编译时错误消息:
' HttpContent不包含ReadAsMultipartAsync的定义'
WebApi的整体项目结构似乎在VS2015中得到了改进。在这一点上,我不知道是什么错(无论是代码问题还是VS2015问题)。我花了最近几天试图解决这个问题。任何人都可以提供有关我做错的见解吗?
以下是我的控制器:
[Route("api/[controller]")]
public class UploadController : Controller
{
private readonly IApplicationEnvironment appEnvironment;
public UploadController(IApplicationEnvironment appEnvironment)
{
this.appEnvironment = appEnvironment;
}
// POST api/values
[HttpPost]
public void Post(HttpRequestMessage request)
{
string logFolder = "logs";
string fileName = "uploader.log";
string logFile = Path.Combine(this.appEnvironment.ApplicationBasePath, logFolder, fileName);
string directory = Path.GetDirectoryName(logFile);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using (Stream requestStream = GetStreamFromUploadedFile(request))
{
using (Stream fileStream = System.IO.File.Create(logFile))
{
try
{
requestStream.CopyTo(fileStream);
}
finally
{
// No longer supported?
//fileStream.Close();
//requestStream.Close();
}
}
}
}
private static Stream GetStreamFromUploadedFile(HttpRequestMessage request)
{
IEnumerable<HttpContent> parts = null;
Task.Factory
.StartNew(() => parts = request.Content.ReadAsMultipartAsync().Result.Contents,
CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default)
.Wait();
Stream stream = null;
Task.Factory
.StartNew(() => stream = parts.First().ReadAsStreamAsync().Result,
CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default)
.Wait();
return stream;
}
}
答案 0 :(得分:1)
以下是如何在ASP.NET 5 / MVC 6中执行文件上传的示例: https://github.com/aspnet/Mvc/blob/dev/test/WebSites/ModelBindingWebSite/Controllers/FileUploadController.cs#L16
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Http;
using Microsoft.Net.Http.Headers;
using ModelBindingWebSite.Models;
namespace ModelBindingWebSite.Controllers
{
public class FileUploadController : Controller
{
public FileDetails UploadSingle(IFormFile file)
{
FileDetails fileDetails;
using (var reader = new StreamReader(file.OpenReadStream()))
{
var fileContent = reader.ReadToEnd();
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
fileDetails = new FileDetails
{
Filename = parsedContentDisposition.FileName,
Content = fileContent
};
}
return fileDetails;
}
public FileDetails[] UploadMultiple(IEnumerable<IFormFile> files)
{
var fileDetailsList = new List<FileDetails>();
foreach (var file in files)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
using (var reader = new StreamReader(file.OpenReadStream()))
{
var fileContent = reader.ReadToEnd();
var fileDetails = new FileDetails
{
Filename = parsedContentDisposition.FileName,
Content = fileContent
};
fileDetailsList.Add(fileDetails);
}
}
return fileDetailsList.ToArray();
}
public IDictionary<string, IList<FileDetails>> UploadMultipleList(IEnumerable<IFormFile> filelist1,
IEnumerable<IFormFile> filelist2)
{
var fileDetailsDict = new Dictionary<string, IList<FileDetails>>
{
{ "filelist1", new List<FileDetails>() },
{ "filelist2", new List<FileDetails>() }
};
var fileDetailsList = new List<FileDetails>();
foreach (var file in filelist1.Concat(filelist2))
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
using (var reader = new StreamReader(file.OpenReadStream()))
{
var fileContent = reader.ReadToEnd();
var fileDetails = new FileDetails
{
Filename = parsedContentDisposition.FileName,
Content = fileContent
};
fileDetailsDict[parsedContentDisposition.Name].Add(fileDetails);
}
}
return fileDetailsDict;
}
public KeyValuePair<string, FileDetails> UploadModelWithFile(Book book)
{
var file = book.File;
var reader = new StreamReader(file.OpenReadStream());
var fileContent = reader.ReadToEnd();
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
var fileDetails = new FileDetails
{
Filename = parsedContentDisposition.FileName,
Content = fileContent
};
return new KeyValuePair<string, FileDetails>(book.Name, fileDetails);
}
}
}