简体   繁体   English

使用jQuery将本地MP3文件发送到API服务器

[英]sending local mp3 file to api server using jquery

I wish to use echoprint - http://echoprint.me/start - which allows me to send an mp3 file locally from my computer in a post request, and returns a json object including the song's details from their server. 我希望使用echoprint- http ://echoprint.me/start-它允许我在发布请求中从计算机本地发送mp3文件,并从其服务器返回一个json对象,其中包括歌曲的详细信息。

I am attempting to make this post request using jquery in order to allow me retrieve the json object containing the song details, which will then allow me view this in my browser's console. 我试图使用jquery发出此发布请求,以便允许我检索包含歌曲详细信息的json对象,然后将其允许我在浏览器的控制台中查看。

The echoprint website - http://developer.echonest.com/docs/v4/track.html - explains how to make this post request using curl. echoprint网站-http: //developer.echonest.com/docs/v4/track.html-说明了如何使用curl发出此发布请求。 The following code works in the command line. 以下代码在命令行中有效。 This returns a json object, however this gets returned in the terminal. 这将返回一个json对象,但是会在终端中返回它。

curl -F "api_key=#############" -F "filetype=mp3" -F "track=@tambourineMan.mp3" "http://developer.echonest.com/api/v4/track/upload"

I have read the curl docs http://curl.haxx.se/docs/httpscripting.html#POST to try and understand where the correlation exists between the curl and jquery, but unfortunately I am having difficulties relating the two and understanding what -F means. 我已经阅读了curl文档http://curl.haxx.se/docs/httpscripting.html#POST,以尝试了解curl和jquery之间的相关性,但不幸的是,我在将两者与关联以及理解问题上遇到了困难- F表示。

My aim is to make this post request using jquery so I can make the same request as outlined using curl above, and retrieve the json data in the browser's console. 我的目的是使用jquery发出此发布请求,这样我就可以进行与上述curl相同的请求,并在浏览器的控制台中检索json数据。

From a previous question I asked on here I have tried to adopt the logic from that answer and used the following code, however this returns an error that the file cannot be encoded. 根据我在此处提出的上一个问题,我尝试采用该答案中的逻辑并使用了以下代码,但是这返回了一个错误,即文件无法编码。 I have tried it with and without the content type specified, but both methods fail. 我尝试了有没有指定内容类型的尝试,但是两种方法都失败了。

   $.post("http://developer.echonest.com/api/v4/track/upload", {
              "api_key":"##################",
              "track":"@tambourineMan.mp3",
              "filetype":"mp3",
              "contentType:" "application/octet-stream"
          },
          function( data ) {
                  console.log(data)
          },
          "JSON" );

There are instructions here http://developer.echonest.com/docs/v4/track.html but they only explain how to do this using curl. 这里有http://developer.echonest.com/docs/v4/track.html上的说明,但是它们仅说明了如何使用curl进行此操作。 If anyone could shed any light on this it would be greatly appreciated. 如果有人能对此有所启示,将不胜感激。 Pardon my ignorance in advance. 请原谅我的无知。

cURL uses the @ prefix to mean "the contents of the named file", in your AJAX request you are sending @tambourineMan.mp3 as a literal string . cURL使用@前缀表示“命名文件的内容”,在AJAX请求中,您将@tambourineMan.mp3作为原义字符串发送。

One easy to way to accomplish your task is to put a file input in your document and tell jQuery to use the data from that file: 一种简单的完成任务的方法是在文件中输入文件,并告诉jQuery使用该文件中的数据:

var file = document.getElementById('myFileInput').files[0];
$.post("http://developer.echonest.com/api/v4/track/upload", {
    "api_key":"##################",
    "track":file,
    "filetype":"mp3",
    "contentType:" "application/octet-stream"
});

Take a look at the FileReader API and at this article about sending and receiving binary data in a XMLHttpRequest 看看的的FileReader API ,并在文章关于发送和接收二进制数据XMLHttpRequest

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

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