[英]Uploading image to Parse using Javascript(CoffeeScript) and REST API
I'm trying to upload images to parse and later attach them to models, however whenever I upload one it returns as a successful upload but the url contains a broken image link. 我正在尝试上传要解析的图像,然后将它们附加到模型中,但是,每当我上传一个图像时,它都会作为一次成功的上传返回,但URL包含损坏的图像链接。
for instance: 例如:
http://files.parse.com/aac0413c-eada-4602-9451-2ee5da7d1241/22eaa50b-1e61-4744-abf9-a57ba9f4123f-test.jpg http://files.parse.com/aac0413c-eada-4602-9451-2ee5da7d1241/22eaa50b-1e61-4744-abf9-a57ba9f4123f-test.jpg
Here's the upload code: 这是上传代码:
getImg: ->
CameraHelper.fileUpload (file) =>
@file = file
forge.file.URL file, (url) =>
@fileURL = url
@$("#uploadImg").addClass("fadeIn").css("background-image", "url(#{url})")
@$("#removeImg").css("display", "inline")
, (content) ->
error "Error finding Image"
, ->
debug "Upload Cancelled"
serverUrl = 'https://api.parse.com/1/files/test.jpg'
parseFile = _.extend @file,
type: "image/jpeg"
name: "share.jpg"
$.ajax
type: "POST",
beforeSend: (request)->
request.setRequestHeader "X-Parse-Application-Id", 'MY-APP-ID'
request.setRequestHeader "X-Parse-REST-API-Key", 'MY-REST-API-ID'
request.setRequestHeader "Content-Type", "image/jpeg"
url: serverUrl
data: parseFile
processData: false
contentType: false
success: (data) ->
alert "File available at: " + data.url
error: (data) ->
obj = jQuery.parseJSON(data)
alert obj
CameraHelper =
fileUpload: (success, err) ->
if APP
forge.file.getImage
saveLocation: "file"
source: "camera"
height: "620px"
width: "620px"
, (file) ->
debug "Successfully uploaded img"
success?(file)
, (content) ->
error "Error in uploading img", content
err?()
else
debug "Sorry that feature is not currently available on the mobile web."
CameraHelper NOTE: I'm using triggerIO, also referenced: https://www.parse.com/questions/uploading-files-to-parse-using-javascript-and-the-rest-api to no avail CameraHelper注意:我使用的是triggerIO,也被引用为: https ://www.parse.com/questions/uploading-files-to-parse-using-javascript-and-the-rest-api无济于事
parseFile is the image I'm trying to upload parseFile是我要上传的图片
I'm not sure exactly what Parse are expecting in the POST body, but I think they want the entire body to be the image data, with no multi-part encoding. 我不确定Parse在POST正文中期望什么,但是我认为他们希望整个正文都是图像数据,没有多部分编码。
That means you need to do two things: 这意味着您需要做两件事:
Firstly, when uploading files, you should use the files
parameter, rather than data
. 首先,上传文件时,您应该使用
files
参数,而不是data
。 See https://trigger.io/docs/current/api/modules/request.html#forgerequestajaxoptions . 请参阅https://trigger.io/docs/current/api/modules/request.html#forgerequestajaxoptions 。 This is true whenever you're uploading files, not just with Parse.
无论何时上传文件,不仅是使用Parse,都是如此。
Secondly, as I think Parse don't want an encoded POST body, use the fileUploadMethod: "raw"
parameter to just dump the image data directly into the request. 其次,由于我认为 Parse不需要编码的POST正文,因此请使用
fileUploadMethod: "raw"
参数将图像数据直接转储到请求中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.