使用dropzone.js发送附加参数

Sah*_*lık 19 javascript dropzone.js

我试图添加dropzone.js,我想传递另一个参数与文件,所以我把隐藏的输入放在窗体中.我可以上传文件,可以用Java部分阅读,但是我看不懂type_chooser,

  ------WebKitFormBoundaryZxF6MCYJpTOLUokN
 Content-Disposition: form-data; name="type_chooser"

 2
 ------WebKitFormBoundaryZxF6MCYJpTOLUokN
 Content-Disposition: form-data; name="file"; filename="isci.xlsx"
 Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Run Code Online (Sandbox Code Playgroud)

所以,如果我写;

 request.getParameter("type_chooser");
Run Code Online (Sandbox Code Playgroud)

我得到了

我怎样才能获得type_chooser?

注意:我试过;

  dropzone.on("sending,function(file,xhr,data){
     data.append("type_chooser","1");
  });
Run Code Online (Sandbox Code Playgroud)

这提供了dropzone形式的隐藏字段的相同输出,它们都发送type_chooser但我无法在java中读取它

Ice*_*man 27

您可以将数据与formdata一起附加

 $("div#dropzone_profile_photo").dropzone({
            url: "/file-upload/",
            init: function() {
                this.on("sending", function(file, xhr, formData){
                        formData.append("data", "loremipsum");
                });
            }
        });
Run Code Online (Sandbox Code Playgroud)

$("div#dropzone_profile_photo").dropzone({
  url: "/test",
  init: function() {
    this.on("sending", function(file, xhr, formData) {
      formData.append("data", "loremipsum");
      console.log(formData)
    });
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/enyo/dropzone/master/dist/dropzone.js"></script>
<link rel="stylesheet" href="https://rawgit.com/enyo/dropzone/master/dist/dropzone.css">
<div id="dropzone_profile_photo" style="width:400px;height:400px; background-color:blue"></div>
Run Code Online (Sandbox Code Playgroud)

  • 迄今为止最干净的解决方案!其他解决方案在URL中添加了参数,浏览器和负载平衡器可能会对其进行缓存(在那里,很容易调试)。将其用作POST表单参数是完美的。 (2认同)

Ada*_*dam 13

我和SahinYanlık相似

var myDropzone = new Dropzone(dropzoneid,
            {
                url: "/files/post",
                acceptedFiles: 'image/*',
                paramName: "file", // The name that will be used to transfer the file
                maxFilesize: 1, // MB
                maxFiles: 1,
                init: function () {

                    // Using a closure.
                    var _this = this;

                    // Setup the observer for the button.
                    $("#clear-dropzone").on("click", function () {
                        // Using "_this" here, because "this" doesn't point to the dropzone anymore
                        _this.removeAllFiles();
                        // If you want to cancel uploads as well, you
                        // could also call _this.removeAllFiles(true);
                    });
                    //this.on("maxfilesexceeded", function (file)
                    //{
                    //    this.removeFile(file);
                    //});
Run Code Online (Sandbox Code Playgroud)

START(这是发送附加数据的覆盖)

                    this.on("sending", function(file, xhr, data) {
                        data.append("filetype", "avataruploadtype");
                    });
Run Code Online (Sandbox Code Playgroud)

结束

                    this.on("addedfile", function() {
                        if (this.files[1] != null) {
                            this.removeFile(this.files[0]);
                        }
                    });
                    this.on("removedfile", function (file) {
                        //html manipulation to disable and select certain page stuff
                    });
                    this.on("success", function (file) {
                         //html manipulation to disable and select certain page stuff                    });
                },
                accept: function (file, done) {
                    if (file.name == "justinbieber.jpg") {
                        done("Naha, you don't."); //just in case!!!!
                    } else {
                        //console.log("done!!!");
                        console.log(done());
                    }
                },
                headers: { "avatar": "avatarupload" }, //you might be also to piggyback of the headers in serverside
                uploadMultiple: false,
                clickable: true,
                addRemoveLinks: true,
            });
Run Code Online (Sandbox Code Playgroud)


小智 9

嗨我有同样的问题,经过大量的研究发现这个解决方案,为我工作我使用jQuery dropzone.

$("#dropzone").dropzone({
    url: "abcd.php",
    dictDefaultMessage: "Drop files here or<br>click to upload...",
    params: {'param_1':'xyz','para_2':'aaa'}
});
Run Code Online (Sandbox Code Playgroud)

params选项是我发现使用dropzone发送附加数据的选项.在params选择指标的影响在接收$_POST和上传文件$_FILE.

希望这可以帮助.


har*_*are 8

尝试将其添加到您的获取参数列表中,如下所示

<form action="/UnitSummary/UploadFile?param1=value&param2=value" class="dropzone" enctype="multipart/form-data" id="imgUpload" method="post">
    <div class="fallback">
        <input name="file" type="file" multiple />
        <input type="submit" value="Upload" />
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)