使用Javascript(CoffeeScript)和REST API将图像上传到Parse [英] Uploading image to Parse using Javascript(CoffeeScript) and REST API

查看:98
本文介绍了使用Javascript(CoffeeScript)和REST API将图像上传到Parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传图像以进行解析,然后将它们附加到模型中,但是,每当我上传一个图像时,它都会作为一次成功的上传返回,但是URL包含损坏的图像链接.

例如:

http: //files.parse.com/aac0413c-eada-4602-9451-2ee5da7d1241/22eaa50b-1e61-4744-abf9-a57ba9f4123f-test.jpg

这是上传代码:

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:

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆