2014-05-05 112 views
2

我想添加一個新的對象,其中包含一列圖像,但它不會保存我的圖片,我的代碼中是否有任何錯誤?特別是保存圖像的一部分!如何通過JavaScript將圖像保存在Parse.com中?

我的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> 

在這裏,我加入HTML線上傳文件:

<input type="file" id="profilePhotoFileUpload"> 
+0

@ A.Wolff你有什麼建議嗎? – user3602314

+0

@Handsomeguy你有什麼建議嗎? – user3602314

+0

這個問題是根據代碼發佈回答後編輯的,Timothy的答案依然正確:先保存你的Parse文件,然後(在該Promise的回調中)保存你的對象 – Sandra

回答

2

檢查documentation here(在該節結束,只是一個有關檢索文件之前)。基本上這個問題就像你需要首先保存它的任何其他Parse對象一樣,然後在保存完成後你可以使用它。

創建該文件並保存,然後在保存success處理程序中,然後可以使用該文件的引用保存該對象。

更新:這是怎樣的代碼上面可以固定:

Parse.initialize("key", "key"); 

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

var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE="; 
var file = new Parse.File("mypic.png", { base64: base64 }); 
file.save({ 
    success: function(file) { 
     alert('File saved, now saving product with file reference...'); 

     var prod = new products(); 
     // to fill the columns 
     prod.set("productID", 1337); 
     prod.set("price", 10); 
     //I guess it need some fixing here 
     prod.set("picture", file); 
     prod.set("productName", "shampoo"); 
     prod.set("productDescribe", "200 ml"); 

     prod.save(null, { 
      success: function(prod) { 
       // Execute any logic that should take place after the object is saved. 

       alert('New object created with objectId: ' + prod.id); 
      }, 
      error: function(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); 
      } 
     }); 
    }, 
    error: function(error) { 
     alert('Failed to save file: ' + error.description); 
    } 
}); 
+0

感謝您的回答,但我仍然忍受着如何做它,我跟着解析,com指南,但它仍然不能成功工作...如果你有一個示例代碼,我會感謝全部 – user3602314

+0

oooh我想編輯我的代碼,但錯誤我做你的回答我我不能刪除,但如果你可以小雞它發現我的錯誤,我會感激..它的運行,但不保存任何文件 – user3602314

+0

非常感謝,我得到了答案,並在這裏發佈它作爲答案,謝謝你的幫助@TimothyWalters – user3602314

7

我有我與您分享它的答案也許有人得利

THML線上傳的文件是:

<input type="file" id="pic"> 

<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(); 
    } 
+2

產品保存需要*在parseFile.save結果函數中 – Sandra