简体   繁体   中英

dropzone.js and ASP.NET MVC file posted is null?

I am trying to use dropzone.js to upload images in my ASP.NET MVC app. I am able to set-up the dropzone programatically, click on it, select the image and when I click "Open" in the file dialog, the correct Action is hit in the controller. However, the HttpPostedFileBase always comes back as null so I can't do anything with the image. ON the client-side, however, it shows the image thumbnail correctly, eventhough I can't get it on the server side.

This is the HTML:

<div style="float:left;margin-right:2px;" id="mainImage">
    <img src="/images/AddImage_button.png" class="dz-message" />
</div>

This is the js code I call after the doc is ready:

var myDropzone = new Dropzone("div#mainImage", { url: "/Market/UploadImage" });

And this is the action call inside of the controller:

public ContentResult UploadImage(HttpPostedFileBase imageFile)
{

    if (imageFile == null || imageFile.ContentLength == 0)
    {
    //.....
    }
}

The action is hit but imageFile is null. Does anyone has any ideas? By the way, the "dz-message" class was added in the image placeholder inside the dropzone because before that it was not clickable. I read it somewhere that was a fix for that issue and it worked.

Any ideas why I am getting null for imageFile?

Default parameter name that Dropzone uses is file , and yours is imageFile . Change imageFile to file and it will work

There is a small tweak you may miss.

Happened to me lots of times,

The form element you use, must have it's enctype attribute set like this:

<form action="~/Home/SaveUploadedFile" method="post" enctype="multipart/form-data"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM