目前,我正在爲圖像文件獲取輸入的表單上工作。瀏覽圖片後上傳,我會得到圖片的ID。這是我的POST代碼。如何使用相同的ID但不同的內容進行更新?
$("#smallpicture_id").change(function() {
displayAndShowImage(this,'#smallimg','#smallimg');
});
$("#largepicture_id").change(function() {
displayAndShowImage(this,'#largeimg','#largeimg');
});
function displayAndShowImage(input,targetHtmlElementName) {
if (input.files && input.files[0]) {
var files = input.files;
var reader = new FileReader();
reader.onload = function (e) {
$(targetHtmlElementName).attr('src', 'images/uploading.gif');
var formData = new FormData();
formData.append('userfile',files[0],files[0].name);
createImage(
config,
formData,
{
onSuccess : function(data) {
$(targetHtmlElementName).attr('src', e.target.result);
$.cookie(input.id, data);
console.log("Image has been save - Received ID: " + data + " saved in the cookie " + input.id);
},
onError : function(jqXHR, status) {
$(targetHtmlElementName).attr('src', 'images/img-error.png');
console.log("ERROR " + jqXHR.responseText + "\r\nstatus = " + status);
}
}
);
}
reader.readAsDataURL(files[0]);
}
}
阿賈克斯
function createImage(cfg,formData,callbacks) {
var xhr = new XMLHttpRequest();
xhr.open('POST', cfg.url + "/image/", true);
xhr.onload = function() {
if (xhr.status === 200) {
// File(s) uploaded.
callbacks.onSuccess(xhr.responseText.trim());
} else {
callbacks.onError(xhr);
}
};
xhr.send(formData);
}
我的問題是我怎麼可以更新/使用該圖像給出相同的ID刪除我的形象。我已經可以做POST和GET了,但我仍然不知道如何更新和刪除。
謝謝,我會稍後再嘗試,並通知您是否有任何問題。 –
你可以真正顯示這樣做嗎? –
@ M.AmirQ從其他答案看來,你似乎需要客戶端腳本來更新那裏只在瀏覽器中的圖像(你有服務器端代碼就地維護數據庫)。你能否澄清我一次 –