user2673206
user2673206

Reputation: 215

cloudinary direct upload form is not valid, need to re upload

i am using django and cloudinary direct upload to upload iamges it is working if the form is valid.

but if my form is not valid, my page reloaded, and then the form will ask the user to upload the image again.

any way to solve this?


my code is attached below. since i use direct upload, after an image is uploaded. it will be displayed immediately on the front end. but when the form is invalid and the form is refreshed, the image is gone. ( after your latest update, it is true i do not need to re upload the photo again. but the photo is gone. how to display that again?)

thanks.

    <script>
        $('.cloudinary-fileupload').bind('cloudinarydone', function(e, data) {
            var imag_var = $.cloudinary.image(data.result.public_id, {
                format : data.result.format,
                version : data.result.version,
                transformation : 'form_preview'
            });
            $('.preview').html(imag_var);
            $('.status_value').text('Uploaded:' + data.result.public_id);
            $('#id_cover_image').val(data.result.public_id);
            return true;
        });
        $('.cloudinary-fileupload').bind('fileuploadprogress', function(e, data) {
            $('.progress').attr({
                style : "visibility:show",
            });
            $('.status_value').text('Uploading...');
        });
    </script>

Upvotes: 0

Views: 514

Answers (1)

Itai Benari
Itai Benari

Reputation: 61

The Cloudinary javascript plugin puts the preloaded resource URI in a hidden field. See here.

From the above one sees that if the field already exists the javascript plugin will populate it. Otherwise, it will create it.

In order to retain that URI across invalid form submissions you should create a hidden field named according to the field name you specified for cloudinary_direct_upload_field and which is populated with the value from the failed submission.

Another way to work around the issue would be to submit the form using an AJAX call. That way the data on page is retained even if validation failed and resubmission is possible. See here for an example of how that may be done.

EDIT:

Version 1.0.12 of the Cloudinary Python client library now adds the hidden field when needed out of the box so resubmissions should work. See the change log.

EDIT 2:

In order to regenerate a preview image in case of form validation error you can add something like this to the javascript code you run on page load (assuming the field name in the form is "image"):

//If form validation failed have the posted preloaded URI from the earlier
//submission so we can show preview
//pull the value from the hidden field
var imageUri = $('#direct_upload input[name="image"]').val();
//preloaded image URI structure
//{resource_type}/{type}/v{version}/{filename}#{signature}
var regex = /^([^\/]+)\/([^\/]+)\/v(\d+)\/([^#]+)#([^\/]+)$/;
//if there is value and it matches the pattern:
if (imageUri && regex.test(imageUri)) {
  var uriParts = regex.exec(imageUri);
  var version = uriParts[3];
  var filename = uriParts[4];
  //reconstruct a viable source
  var src = "v" + version + "/" + filename;
  //generate a cloudinary image tag and add it to the 'preview' div
  $(".preview").append(
    $.cloudinary.image(src, {
      transformation: "form_preview" //your named transformation
    })
  );
}

Upvotes: 1

Related Questions