在ASP.NET中使用5 System.Net.Http方法呢? [英] Using System.Net.Http methods in ASP.NET 5?

查看:412
本文介绍了在ASP.NET中使用5 System.Net.Http方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为移动客户端的API,并使用ASP.NET 5.一切都顺利的AngularJS Web前端,但我难倒了如何创建一个POST将文件上传到服务器。

I'm writing an API for mobile clients and an AngularJS web front end using ASP.NET 5. Everything's going well, but I'm stumped on how to create a POST to upload files to the server.

我发现最好的方式是在<一个href=\"http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi\">this螺纹。基于研究,我已经做了,我要像做以下,但我一直用VS2015得到击落,这是无法找到适当的引用。

The best info I've found is in this thread. Based on the research I've done, I want to do something like the following, but I keep getting shot down by VS2015 and it's inability to find proper references.

    [HttpPost("card/{cardId}/image")]
    public async Task<IActionResult> Upload()
    {
        var provider = new MultipartMemoryStreamProvider();
        await Request.Body.ReadAsMultipartAsync(provider);
        foreach (var file in provider.Contents)
        {
            var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
            var buffer = await file.ReadAsByteArrayAsync();
            //Do whatever you want with filename and its binaray data.
        }
        return Json (new { Message = "Hooray!" });
    }

...这是VS2015是什么使得它的:

...and here's what VS2015 makes of it:

VS2015问题

使用DNXcore5.0似乎MultipartMemoryStreamProvider这是<一部分href=\"https://msdn.microsoft.com/en-us/library/system.net.http.multipartmemorystreamprovider(v=vs.118).aspx\"相对=nofollow> System.Net.Http 无法在命名空间中找到,并且Request.Body不ReadAsMultipartAsync(这不能被发现无论如何努力,因为它是在System.Net .Http空间...)

With DNXcore5.0 it seems that MultipartMemoryStreamProvider which is part of System.Net.Http can't be found in the namespace, and Request.Body doesn't work with ReadAsMultipartAsync (which couldn't be found anyway, because it's in that System.Net.Http space...)

有没有更好的方式来建立一个API POST在ASP.NET 5接受文件?或者,我们现在怎么办引用之类的东西System.Net.Http已经工作多年了?

Is there a better way to set up an API POST to accept files in ASP.NET 5? Or how do we now reference things like System.Net.Http that have worked for years?

推荐答案

在MVC6,您可以使用 IFormFile 从您的Web应用程序处理文件上传。

In MVC6, you may use IFormFile to handle file upload from your web app.

public IActionResult UploadFiles(IEnumerable<IFormFile> logos)
{
    foreach (var file in logos)
    {
        if (file != null)
        {
           var fileName = string.Empty;

          //If you want to get file name etc, You can read it from the ContentDisposition
          var c= logo.ContentDisposition;
          var meta = c.Split(';').ToList();
          var fileNameMeta = meta.FirstOrDefault(s => s.StartsWith(" filename"));
          if(!String.IsNullOrEmpty(fileNameMeta))
          {
             fileName = fileNameMeta.Split('=')[1];
          }
          logo.SaveAs("MyUploads/myUniquefileName.png");
        }
    }
    return View(); // or some JSON as needed
}

假设你输入文件元素的名称为标志

<form asp-action="UploadFiles" asp-controller="Home" enctype="multipart/form-data">
    <input type="file" name="logos" />
    <input type="file" name="logos" />
    <input type="submit" />
</form>

IFormFile Microsoft.AspNet.Http 命名空间这是在定义微软.AspNet.Http.Abstractions 组装。

IFormFile is defined in the Microsoft.AspNet.Http namespace which is in Microsoft.AspNet.Http.Abstractions assembly.

如果你想要做的通过AJAX的文件上传,看看这个答案

If you want to do the file upload via ajax, take a look at this answer.

和为您的移动应用程序,你可能需要将文件转换为字符串的base64(您的移动应用程序内),并传送给你的操作方法。你会读到的字符串值,然后从base64string将其转换回ByteArray或文件。

And for your mobile app, You probably need to convert the file to a base64 string (inside your mobile app) and send it to your action method. You will read the string value and convert it back to bytearray or file from the base64string.

这篇关于在ASP.NET中使用5 System.Net.Http方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆