FormData with OData Web API

I have successfully placed download / upload orders using the vanilla web browser, but now I am trying to consolidate my logic on ODataControllers, so these download / download calls are partly in the same controller that returns the list of files.

To do this, I did the following:

[HttpPost]
public Task<IEnumerable<FileComponent>> Upload()
{
    try
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        var provider = new MultipartFormDataStreamProvider(root);

        return Request.Content.ReadAsMultipartAsync(provider).
            ContinueWith<IEnumerable<FileComponent>>(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, t.Exception));
                }

                var fileComponents = new List<FileComponent>();
                foreach (MultipartFileData file in provider.FileData)
                {
                    string filename = file.Headers.ContentDisposition.FileName.Replace("\"", "");
                    var filepath = root + filename;
                    if (File.Exists(filepath))
                    {
                        // If previous entry exists, delete
                        File.Delete(filepath);
                    }

                    // The file is saved with a random GUID for name. Can rename it here.
                    string guidFilepath = file.LocalFileName;
                    File.Move(guidFilepath, filepath);

                    fileComponents.Add(new FileComponent()
                    {
                        Name = filename,
                        Size = new FileInfo(filepath).Length
                    });
                }
                return fileComponents;
            });
    }
    finally
    {
        log.Info("Exiting Post");
    }
}

However, it never works. It always comes with the absence of FormData in MultipartFormDataStreamProvider, and the exception is caught and returned as "Unexpected end of the multithreaded MIME stream. MIME multipart message is not complete. '.

Any ideas? I assume OData is clearing it.

+3
source share

All Articles