[英]Rename uploaded file so it can be removed DropZone.js
我已經決定更新這個問題,因為我仍然無法更改imageId
這是我目前的dropzone實現
$("div#UploadImage").dropzone({
url: '@Url.Action("SaveUploadedPhoto", "Listing", new { advertId = @Model.AdvertId })',
addRemoveLinks: true,
init: function () {
var myDropzone = this;
// First change the button to actually tell Dropzone to process the queue.
$("input[type=submit]").on("click", function (e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
myDropzone.processQueue();
});
myDropzone.on("success", function (file, rep) {
console.log(rep.UniqueId);
fileList[i] = { "serverFileName": rep.UniqueId, "fileName": rep.UniqueId, "fileId": rep.UniqueId, "name": rep.UniqueId };
i++;
console.log(fileList);
});
myDropzone.on("removedfile", function (file) {
var rmvFile = "";
for (f = 0; f < fileList.length; f++) {
if (fileList[f].fileName == file.name) {
rmvFile = fileList[f].serverFileName;
}
}
if (rmvFile) {
$.ajax({
url: '@Url.Action("DeleteUploadedFile", "Listing")',
type: "POST",
data: { "id": rmvFile }
});
}
});
},
maxFilesize: 2,
maxFiles: 12
});
當我上傳圖片時,我們將圖片傳遞給第三方公司,該公司為該圖片返回uniqueId,然后將這個uniqueId保存在我的數據庫中,然后將此uniqueId傳遞回視圖,該視圖將成為上傳的圖像名稱,I當我這樣做時,我試圖在dropzone的上述實現中的“成功”功能中這樣做
console.log(rep.UniqueId);
我可以看到價值及其正確。
當我在dropzone上按“刪除圖像”時,會調用“removedFile”函數,這不會激活到控制器,因為rmvFile是舊的圖像名稱,並且它在for循環中找不到匹配項,我知道這是因為當我做
console.log(rmvFile);
它顯示了舊名稱,現在我的問題是我在這里做錯了什么? 如何將上傳的圖像名稱或ID更改為上傳完成后返回的uniqueId? 所以我可以在需要時成功刪除它。
我瀏覽了網頁,嘗試過的情況導致我的成功方法目前看起來如何。
在嘗試了許多不同的解決方案之后,我已經設法讓這個工作了。
我發現在圖像的uniqueId中進行以下傳遞
file.serverId['UniqueId'],
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.