12

I'm using dropzone.js to upload files in a form that include other fields.

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.DropDownListFor(x => x.installation, Model.installationList, new { data_placeholder = "Select one item please" })
    @Html.ValidationMessageFor(model => model.installation, "", new { @class = "text-danger" })

<div id="files" name="files" class="dropzone"></div>

<input type="submit" value="@Resources.Global.Save" class="btn btn-default" />
}

JS:

Dropzone.options.files = {
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 100,
            maxFiles: 100,

            paramName: "files", // The name that will be used to transfer the file
            maxFilesize: 8, // MB
            url: "/ActionPlan/Home/Create"  // Same as URL generated from the form
        };

My model:

        // installation 
        [Display(Name = "Anomaly_Installation", ResourceType = typeof(Resources.ActionPlan))]
        public int installation { get; set; }
        public IEnumerable<SelectListItem> installationList { get; set; }

// files uploaded
        public HttpPostedFileBase[] files { get; set; }

When I submit the form, no files are attached to the model, but data from location is OK, why? How to fix this issue?

EDIT: I've made some changes but same issue:

HTML (Razor)

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @class = "dropzone", id = "myForm" }))

And I've added:

<div class="dropzone-previews"></div>
                <div class="fallback">
                    <!-- this is the fallback if JS isn't working -->
                    <input name="files" type="file" multiple />
                </div>

JS

Dropzone.options.files = {
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 25,
            maxFiles: 25
        };

When I inspect headers sent, I didn't see any files (this is the entire form):

------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="__RequestVerificationToken"

hQJmhZpJf0LqOo3ZZCgCUjMafbXdjNGmzM8QrnL2bjtWUerKZiyJakNJljNsM_DowRv5641qUyc0zjRcBIUh2I1AZ2LBBYko8UzrhPFvnzeWELBVBLwTmtfo6KUX5MChk_aIKvX-aEcpremYXJps1A2
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyId"

0
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="beginDate"

09/04/2015
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomaly"

wsqfdgsqdfsqz
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="analysis"

wsdwsdfg
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyTypeSelected"

2
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="piloteSelected"

52333
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyOriginSelected"

3
------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="anomalyOriginData"


------WebKitFormBoundaryAKklxx9XCCYQ22Zl
Content-Disposition: form-data; name="installation"

1
------WebKitFormBoundaryAKklxx9XCCYQ22Zl--

FINAL SOLUTION: HTML:

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @class = "dropzone", id = "myForm" }))
{
...
<div class="fallback">
                    <!-- this is the fallback if JS isn't working -->
                    <input name="files" type="file" multiple />
                </div>
}

JS : Dropzone.autoDiscover = false;

        var myDropzone = new Dropzone('#myForm', {
            paramName: 'files',
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 25,
            maxFiles: 25
        });

        $("form").on("submit", function (event) {
            myDropzone.processQueue(); // Tell Dropzone to process all queued files.
        });

for this im my model:

public HttpPostedFileBase[] files { get; set; }
clement
  • 4,204
  • 10
  • 65
  • 133
  • 1
    You need to define the enctype in the form `Html.BeginForm("action","controller",FormMethod.Post,new{ enctype = "multipart/form-data" })` –  Apr 07 '15 at 09:57
  • 1
    Not familiar with `dropzone` but I suspect `paramName: "file",` would need to be `paramName: "files",` to match your property name –  Apr 07 '15 at 10:03
  • @gerdi: I've made that change but I still have the issue – clement Apr 07 '15 at 10:23
  • @StephenMuecke: thanks again, i've made the change (it looks better) but there still have a problem because files is null in my model in the controller :-( – clement Apr 07 '15 at 10:24
  • 1
    Does `dropzone` create a hidden ``? `Have you tested it without applying `dropzone` (i.e just using `` which should work fine)? –  Apr 07 '15 at 10:29
  • It seems as though dropzone hits the [httpPost] every time an image is added. I just tried it now (with default settings) and the images are getting saved as well as an associated user. However. When i actually hit the save button on the form there is an error because on that request there are no images. Try settings a breakpoint in the [HttpPost] to see this happen , or to maybe see what is not happening –  Apr 07 '15 at 10:47
  • @gerdi: I've used autoProcessQueue: false – clement Apr 07 '15 at 10:50
  • @StephenMuecke: Great idea. I've inspected working "simple" dropzone in my application and there is no hiddeninput. For the manual input type file, it works... – clement Apr 07 '15 at 10:56
  • @StephenMuecke : I've added the network sniffed trough chrome in the initial post, no trace of files... – clement Apr 09 '15 at 13:36
  • Can't comment on `DropZone`, but for the fallback, it needs to be `` (plural - to match your property name) –  Apr 10 '15 at 06:12
  • @StephenMuecke: yes, when I comment the dropzoneJS's javascript, the fallback is working and is binded to the model – clement Apr 10 '15 at 08:26
  • `paramName: "file"` should be `paramName: "files"` – damian Apr 15 '15 at 15:16
  • @enyce : Yes I've already editedt that, thanks, but it doesn't resolve the issue – clement Apr 16 '15 at 06:52

2 Answers2

5

I guess the options you specified never get applied. That would explain why no files are attached to your model once you submit the form as they were already processed on upload. To proper apply the desired options you need to turn off the auto discovery function from Dropzone:

Dropzone.autoDiscover = false;

That way you have to programmatically initialize Dropzone:

var myDropzone = new Dropzone('form', {
    paramName: 'files',
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 25,
    maxFiles: 1
});

Demo

autoProcessQueue

When set to false you have to call myDropzone.processQueue() yourself in order to upload the dropped files. See below for more information on handling queues.

damian
  • 5,314
  • 5
  • 34
  • 63
  • thanks. i edited my code as you suggest (I just used #myForm selector in place of form ) but it doesn't bind the fle uploaded to the model: I didn't receive any files into the "public HttpPostedFileBase[] files { get; set; }" variable that I get in the controller :-/ – clement Apr 16 '15 at 12:55
  • ha yes, I can see the "drop files to upload" so it signify that the js is applied. I have the same headers than in my first post so the dropzone don't do his work, it looks like there is no files included in the HTTPPOST – clement Apr 16 '15 at 13:04
  • I guess the problem is on the client side as the fallback is working properly. Are you calling `processQueue ` manually? If you use `autoProcessQueue = false` you have to do so. – damian Apr 16 '15 at 13:55
  • Thanks but here I did the same than you, autoProcessQueue at false – clement Apr 16 '15 at 14:09
  • Yeah and that's why you have to use `processQueue()` :) Have you? – damian Apr 16 '15 at 14:12
  • bingo! $("form").on("submit", function (event) { myDropzone.processQueue();}); – clement Apr 16 '15 at 14:25
1

1) Open your tool develop console in your browser and put an breakpoint in "dropzone.uploadFile" (see the image: http://www.tiikoni.com/tis/view/?id=033ad74) 2) Drop an image an check if file is no empty

If empty, the error probably are in the script of backend (usually an controller in php, asp, ecc). If is not empty,test an clean version of dropzone (http://www.dropzonejs.com/) and take a look to the differences.

Sorry for my terrible English :)

Victor D.
  • 11
  • 2
  • Hello Victor, Thanks for our ask. I only have "Dropzone.prototype.uploadFiles = function(files) {" in this version of dropzone. No problem with your english, I'm not fluent too :-) Without testing as you want, file that I upload are pictures of 1meg and there is thumbs in the form when I drop them... – clement Apr 16 '15 at 14:19