问题描述
我正在尝试上传图像以进行解析,然后将它们附加到模型中,但是,每当我上传一个图像时,它都会作为一次成功的上传返回,但是URL包含损坏的图像链接.
例如:
这是上传代码:
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注意:我使用的是triggerIO,也被引用为: https://www.parse.com/questions/uploading-files-to-parse-using-javascript-and-the-rest-api 无济于事
parseFile是我要上传的图片
我不确定POST正文中Parse到底期望什么,但是我认为他们希望整个正文都是图像数据,并且不能多部分编码.
这意味着您需要做两件事:
首先,上传文件时,应使用files
参数,而不是data
.请参见 https://trigger.io/docs/current/api/modules/request.html#forgerequestajaxoptions .每当您上传文件时,不仅是使用Parse,都是如此.
其次,由于我认为解析不需要编码的POST正文,请使用fileUploadMethod: "raw"
参数将图像数据直接转储到请求中.
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.
for instance:
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."
CameraHelperNOTE: I'm using triggerIO, also referenced: https://www.parse.com/questions/uploading-files-to-parse-using-javascript-and-the-rest-api to no avail
parseFile is the image I'm trying to upload
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.
That means you need to do two things:
Firstly, when uploading files, you should use the files
parameter, rather than data
. See https://trigger.io/docs/current/api/modules/request.html#forgerequestajaxoptions. This is true whenever you're uploading files, not just with 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.
这篇关于使用Javascript(CoffeeScript)和REST API将图像上传到Parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!