我在这里有其他线程中看到的波纹管代码,但实际上它没有显示如何将MultipartFileData文件保存到磁盘.
[HttpPost]
public Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath(Settings.AssetImageTempStorage);
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
// HOW DO I SAVE THIS FILE TO DISK HERE ??? …Run Code Online (Sandbox Code Playgroud) 我的Asp.net MVC中的任何帖子都会生成此错误:
Server Error in '/' Application.
Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
Source …Run Code Online (Sandbox Code Playgroud) 我正在使用jQuery ajax上传文件,但想在webapi方法上添加一些参数,这里是:
var data = new FormData();
data.append("file", $("#file")[0].files[0]);
data.append("myParameter", "test"); // with this param i get 404
$.ajax({
url: '/api/my/upload/',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (data) {
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
Webapi控制器:
public class MyController : ApiController
{
public string Upload(string myParameter)
{
return System.Web.HttpContext.Current.Request.Files.Count.ToString() + " / " + myParameter;
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有myParameter一切正常,但是当我在formdata和api方法中包含myParameter时,我得到404,有没有机会让它工作?