weixin_33730836 2017-08-29 21:48 采纳率: 0%
浏览 169

将Blob发送到ASP.NET Web API

I'm trying to use a component (slim image cropper) to crop images then send both the original image and the cropped version to my server through a Web API. My problem here is that I get the data as a blob array (first element is the original and the second is cropped) and I have absolutely no idea how to send that data, or how to transform it to be able to send it to my api.

I'm using a javascript function instead of a direct service call to pass more parameters, as supported by the component and documented here.

Here's the function :

var uploadImage = function (formdata, progress, success, failure) {
    var originalArray;
    var croppedArray;

    var apiCall = function (original, cropped) {
        var params = {
            name: formdata[0].name,
            original,
            cropped,
            id: $('hidId').val()
        }
        $.getJSON('/api/Test/UploadImage', params, function (response) {
            if (response) {
                if (response.Success == true) {
                    success('Upload réussi')
                } else {
                    failure('Upload échoué')
                }
            } else {
                failure('Pas de réponse de l\'API')
            }
        });
    };

    var originalReader = new FileReader();
    originalReader.onloadend = function () {
        originalArray = originalReader.result;
        if (originalArray != null && croppedArray != null)
            apiCall(originalArray, croppedArray);
    };
    originalReader.readAsArrayBuffer(formdata[0]);

    var croppedReader = new FileReader();
    croppedReader.onloadend = function () {
        croppedArray = croppedReader.result;
        if (originalArray != null && croppedArray != null)
            apiCall(originalArray, croppedArray);
    };
    croppedReader.readAsArrayBuffer(formdata[1]);
}

I know it must look sketchy, but I have no experience at all with that case. When I get to the $.getJSON(...) line, I get a 404 error in the console, showing that only the "name" parameter is passed.

Just in case it's relevant, here's my api method signature :

public IHttpActionResult UploadImages(string name, byte[] original, byte[] cropped, Guid id)
{
    ...
}

How can I solve this and send the images ? What should be the parameter type on the api side ?

  • 写回答

1条回答 默认 最新

  • weixin_33682790 2017-08-29 22:03
    关注

    You can use a form with the enctype="multipart/form-data" attribute (you can submit that form via ajax)
    And received it as a HttpPostedFileBase type on the server-side.
    For Example taken from here:

    <form action="" method="post" enctype="multipart/form-data">
    
      <label for="file">Filename:</label>
      <input type="file" name="file" id="file" />
    
      <input type="submit" />
    </form>
    
    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file) {
    
      if (file.ContentLength > 0) {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
      }
    
      return RedirectToAction("Index");
    }
    

    EDIT
    For multiple files you can use the input's attribute multiple="multiple"
    and in the server-side accept a collection of HttpPostedFileBase like IEnumerable<HttpPostedFileBase>.

     <input type="file" multiple="multiple" name="files" id="files" />
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中