如何使用Parse.com javascriptSDK上传图像?

时间:2012-09-05 10:35:27

标签: javascript backbone.js parse-platform

我正在使用Parse.com构建一个javascript应用程序,我需要上传照片。我有一个表单,允许用户在他们的文件系统上选择图像,但我接下来该怎么办呢?我在Parse站点上找不到任何相关文档(不管是javascriptSDK,还是其他)。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

以下是有关如何上传图片的简单示例:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

  // ***************************************************
  // NOTE: Replace the following your own keys
  // ***************************************************

  Parse.initialize("YOUR_APPLICATION_ID", "YOUR_CLIENT_ID");

  function saveJobApp(objParseFile)
  {
     var jobApplication = new Parse.Object("JobApplication");
     jobApplication.set("applicantName", "Joe Smith");
     jobApplication.set("profileImg", objParseFile);
     jobApplication.save(null, 
     {
        success: function(gameScore) {
          // Execute any logic that should take place after the object is saved.
          var photo = gameScore.get("profileImg");
          $("#profileImg")[0].src = photo.url();
        },
        error: function(gameScore, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and description.
          alert('Failed to create new object, with error code: ' + error.description);
        }
     });
  }

  $('#profilePhotoFileUpload').bind("change", function(e) {
         var fileUploadControl = $("#profilePhotoFileUpload")[0];
         var file = fileUploadControl.files[0];
         var name = file.name; //This does *NOT* need to be a unique name
         var parseFile = new Parse.File(name, file);

         parseFile.save().then
         (
           function() 
           {
               saveJobApp(parseFile);
           }, 
           function(error) 
           {
             alert("error");
           }
         );
  }); 

});
</script>

<body>
    <input type="file" id="profilePhotoFileUpload">
    <img id="profileImg"/>
</body>

答案 1 :(得分:-2)

好的,我在谷歌搜索之后找到了答案(在我发现谷歌“文件上传”之后,而不仅仅是'图片上传'!)。我用过这个:

http://help.gotiggr.com/documentation/services/examples-connecting-to-mobile-back-ends/parse/file-upload

认为它可能会为其他人派上用场!