相关疑难解决方法(0)

将MultipartFileData文件保存到磁盘

我在这里有其他线程中看到的波纹管代码,但实际上它没有显示如何将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)

multipart multifile-uploader c#-4.0

8
推荐指数
1
解决办法
1万
查看次数

发布错误:在内部存储之前访问了BinaryRead,Form,Files或InputStream

我的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)

asp.net iis-express asp.net-mvc-4

7
推荐指数
4
解决办法
9408
查看次数

带有额外参数的Webapi ajax formdata上传

我正在使用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,有没有机会让它工作?

jquery multipartform-data asp.net-web-api

6
推荐指数
1
解决办法
9413
查看次数