简体   繁体   English

通过Ajax将图像上传到imm.io

[英]Upload image to imm.io via ajax

I have trying to create a web page ussing php and ajax to let users upload files to imm using its API: 我试图创建一个使用php和ajax的网页,以允许用户使用其API将文件上传到imm

http://imm.io/api/ (feel free to take a look because it is just a simple example) http://imm.io/api/ (请看一下,因为它只是一个简单的示例)

I would like to know how to upload dynamically using jquery, the idea is reproduce the workflow below: 我想知道如何使用jquery动态上传,其想法是复制以下工作流程:

  1. the user complete the form 用户填写表格
  2. when the submit button is clicked, it will take the form values and sent them to imm.io 单击“提交”按钮时,它将采用表单值并将其发送到imm.io
  3. wait until the json response is received 等待直到收到json响应
  4. show the error/success message updating a div 显示更新div的错误/成功消息

My code: 我的代码:

var request = $.ajax({
      url: 'http://imm.io/store/',
      type: "POST",
      cache: false,
      data: { ??? },  //here my problem resides
      dataType: "json"
    });

    //waiting ultil the request be finished
    $.when( request ).then(function(data, textStatus, jqXHR){
        console.log(data);
    });

If you note above, the ??? 如果您在上面注明, ??? represents where the problem resides, because I don't know how to sent the file value into the json format thar "data" needs. 表示问题所在,因为我不知道如何将文件值发送为“数据”需要的json格式。

Seeing some questions here in SO looks like ajax is not able to send files. 在SO中看到一些问题看起来像ajax无法发送文件。

So, I just need a hand to guide to the right direction.... One more thing: I never has used the CURL php extension, but I thing that could be another option, right? 因此,我只需要指导正确的方向即可。...还有一件事:我从未使用过CURL php扩展名,但是我有可能是另一种选择,对吗?

EDIT, I forgot tell you why I did not use the plugins mencioned in the questions above: because that plugins don't wait until the request was completed, so, I never get the real response form the server. 编辑,我忘了告诉你为什么我不使用上面问题中提到的插件:因为这些插件不会等到请求完成后再进行操作,因此,我永远不会从服务器获得真正的响应。

I found a good tutorial on this. 我找到了一个很好的教程 Also a jquery plugin 也是一个jQuery插件

Hope that helps ;) 希望能有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM