Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename uploaded file so it can be removed DropZone.js

I have decided to update this question as I still can't change the imageId

This is my current dropzone implementation

   $("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
        });

When I upload an image, we pass the image to a third party company which returns a uniqueId for that image, this uniqueId then gets saved in my database, I then pass this uniqueId back to the view which will become the uploaded image name, I'm trying to do this in the "success" function in the above implementation of dropzone, when I do

console.log(rep.UniqueId);

I can see the value and its correct.

When I press "Remove image" on dropzone the "removedFile" function is called, this doesn't fire to the controller because rmvFile is the old image name and it doesn't find a match within the for loop, I know this because when I do

console.log(rmvFile);  

It shows the old name, now my question is what am I doing wrong here? how can I change the uploaded image name or id to the uniqueId which is returned after the uploaded has been completed? so I can successfully delete it as and when it required.

I have browsed the web, tried situation's which has resulted in how my success method currently looks.

like image 266
Code Ratchet Avatar asked Oct 19 '22 19:10

Code Ratchet


1 Answers

I've managed to get this working now, after trying many different solutions.

I have found doing the following passes in the uniqueId for the image

file.serverId['UniqueId'],
like image 123
Code Ratchet Avatar answered Oct 27 '22 22:10

Code Ratchet