2012-09-05 54 views
3

我正在使用Parse.com构建一个JavaScript应用程序,并且需要上传照片。我有一个允许用户在其文件系统上选择图像的表单,但接下来我该怎么处理它?我无法在Parse网站上找到任何有关此文档的文档(不管JavaScriptSDK如何)。如何使用Parse.com javascriptSDK上传图片?

感谢您的帮助!

回答

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> 
+0

伟大的答案 - 它看起来像你下载的jQuery两次? – CharlesA

+0

@CharlesA - 很棒! –

+0

parseFile.save()保存到什么表? – SuperUberDuper