WebUpload formdata 上傳參數

https://www.cnblogs.com/wisdo/p/6159761.htmlcss

 

webUploader 是款很好用的優秀的開源上傳組件,由百度公司開發,詳細的介紹可參見webUploader 的官方文檔:html

最近在使用webUploader時,須要添加額外的參數,並在後臺獲取,參數的格式是json,剛開始一直設置不對,在參考了官網的api文檔及相關熱心網友的回答後,終於弄明白了webUploader 上傳參數的設置。設置的方法通常是在uploader 對象建立後的 fileQueued  或 uploadBeforeSend 事件 中設置 ,也能夠對webUploader 對象的屬性 formData 進行直接 設置。web

具體以下 :json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    uploader = WebUploader.create({
                    auto: false,
                    // swf文件路徑
                    swf: '/Scripts/webUploader/Uploader.swf',
 
                    // 文件接收服務端。
                    server: '@Url.Action("Upload", "Home")',
 
                    // 選擇文件的按鈕。可選。
                    // 內部根據當前運行是建立,多是input元素,也多是flash.
                    pick: '#myPicker',
 
                   // formData: { "name": name, "desc": desc},
                
                    prepareNextFile:true,
                    chunked: true// 分片上傳大文件
                    chunkRetry: 10, // 若是某個分片因爲網絡問題出錯,容許自動重傳多少次?
                    thread: 100,// 最大上傳併發數
                    method: 'POST',
                    fileSizeLimit: 1024,
 
                    // 只容許選擇圖片文件。
                    accept: {
                        title: 'HTML5組態文件',
                        extensions: 'gif,jpg,jpeg,bmp,png',
                       mimeTypes: 'img/*'
                    }
 
                });
 
        5
6
7
8
9
10
11
12
13
  
 
//當文件被加入隊列以前觸發
uploader.on('beforeFileQueued', function (file) {
    //若是是單文件上傳,把舊的文件地址傳過去
    if (!p.multiple) {
        if (p.sendurl.indexOf("action=itemcode") > 0) {
            if ($("#txtItemCode").val() == '') {
                layer.msg('請先填寫商品編碼再上傳圖片!');
                //layer.alert('請先填寫商品編碼再上傳圖片!');
                return false;
            }
        data.formData= { "name": name, "desc": desc};
        }
    }
    });
 
 
                uploader.on('fileQueued', function (file) {
                    $("#listFile").append('<div id="' + file.id + '" class="item">' +
                        '<h4 class="info">' + file.name + '</h4>' +
                        '<p class="state">等待上傳...</p>' +
                    '</div>');
                });
 
                uploader.on('uploadProgress', function (file, percentage) {
                    var li = $('#' + file.id),
                        percent = li.find('.progress .progress-bar');
                    // 避免重複建立
                    if (!percent.length) {
                        percent = $('<div class="progress progress-striped active">' +
                          '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                          '</div>' +
                        '</div>').appendTo(li).find('.progress-bar');
                    }
                    li.find('p.state').text('上傳中');
                    percent.css('width', percentage * 100 + '%');
                });
 
                uploader.on('uploadSuccess', function (file) {
                    $('#' + file.id).find('p.state').text('已上傳');
                });
 
                uploader.on('uploadError', function (file) {
                    $('#' + file.id).find('p.state').text('上傳出錯');
                });
 
                uploader.on('uploadComplete', function (file) {
                    $('#' + file.id).find('.progress').fadeOut();
                    //$("#editModal").fadeOut(2000, window.location.reload());
                    //destory();
                    //$("#editModal").destory();
                });
 
 
                //當某個文件的分塊在發送前觸發,主要用來詢問是否要添加附帶參數,大文件在開起分片上傳的前提下此事件可能會觸發屢次。
 
                uploader.on('uploadBeforeSend', function (obj, data, headers) {
                   // data.DelFilePath = parentObj.siblings(".upload-path").val();
                   //  data.ItemCode = $("#txtItemCode").val();
             data.formData= { "name": name, "desc": desc};
                });
 
                uploader.on('all', function (type) {
                    if (type === 'startUpload') {
                        state = 'uploading';
                    } else if (type === 'stopUpload') {
                        state = 'paused';
                    } else if (type === 'uploadFinished') {
                        state = 'done';
                    }
                    if (state === 'uploading') {
                        $('#btnBeginUpload').text('暫停上傳');
                    } else {
                        $('#btnBeginUpload').text('開始上傳');
                    }
                });
 
            } else {
                geap.alertPostMsgDefault("請選擇一個文件!", "info");
            }
        });
 
 
   // 點擊上傳事件
 
  $('#btnSave').bind('click', function () {
           var  name = $("#txtName").val();
           var  id = $("#txtId").val();
 
             if (!name || name.length == 0) {
                 alert("請填寫名稱");
                 return false;
             }
            var obj = new Object();
            obj.name = name;
            obj.id = id;
            uploader.options.formData = obj;
          //  uploader.options.formData = { "name": name, "id": id, };
            if (state === 'uploading') {
                uploader.stop();
            } else {
                uploader.upload();
            }
        });
相關文章
相關標籤/搜索