如何通过JavaScript在Parse.com中保存图像?

use*_*314 3 javascript image parse-platform

我想在其中一个列中添加包含Image的新Object,但它不能保存我的Pic,我的代码中是否有任何错误?特别是保存图片的一部分!!

问题出现的我的JavaScript:

它永远不会上传我的照片!

    <script type="text/javascript">
    Parse.initialize("key", "key");


   var products = Parse.Object.extend("products");

    var fileUploadControl = $("#profilePhotoFileUpload")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = "photo.png";

var parseFile = new Parse.File(name, file);
}
parseFile.save().then(function() {
  //The file has been saved to Parse.
   }, function(error) {
   // The file either could not be read, or could not be saved to Parse.
 });

    </script>
Run Code Online (Sandbox Code Playgroud)

这里我添加了html行上传文件:

<input type="file" id="profilePhotoFileUpload">
Run Code Online (Sandbox Code Playgroud)

use*_*314 8

我得到了答案,我与你分享它也许有人得到了好处

THML上传文件的行是:

<input type="file" id="pic">
Run Code Online (Sandbox Code Playgroud)

<script>在解析中获取和保存图像的代码是:

     var fileUploadControl = $("#pic")[0];
   if (fileUploadControl.files.length > 0) {
   var file = fileUploadControl.files[0];
   var name = "photo.png";

   var parseFile = new Parse.File(name, file);

   //put this inside if {
   parseFile.save().then(function() {
   // The file has been saved to Parse.
   }, function(error) {
   // The file either could not be read, or could not be saved to Parse.
    });

    // Be sure of ur parameters name
    // prod is extend of my class in parse from this: var prod = new products();
    prod.set("picture", parseFile);
    prod.save();
   }
Run Code Online (Sandbox Code Playgroud)

  • 产品保存需要*内部*parseFile.save结果函数 (2认同)