HTML
(可多選)
JAVASCRIPT
function NewFileUploader($buttom){ var buttom_id = $buttom.attr('id'); var table_name = $buttom.attr('table_name'); var uploader = new plupload.Uploader({ runtimes: 'html5', browse_button: buttom_id, url: '', multi_selection: true, max_file_size : '100mb', multipart: true, headers: { 'X-Requested-With': 'XMLHttpRequest', }, multipart_params : { csrfmiddlewaretoken: CSRFMIDDLEWARETOKEN, table_name: table_name }, init:{ FilesAdded:function(up, files){ var file_num = files.length; if (confirm('確定上傳這' + file_num + '個檔案?,按下確定後,上傳進度將顯示在下方,尚未結束前請勿關閉視窗!!!')){ for (i=0;i' + files[i].name + ',上傳進度: '); } up.start(); }else{ return false } }, FileUploaded:function(up, file, res){ var json = $.parseJSON(res.response); var data = { 'id': json["id"], 'name': json['name'] }; var $col = $('#li_file').tmpl(data).appendTo($('#upload_files')); $('#li_' + file.id).remove(); $(".deleteRow").unbind('click'); $(".deleteRow").click(deleteRow); }, UploadProgress:function(up, file) { $('#file_percent_' + file.id).attr('style', 'width: ' + file.percent + "%"); $('#file_percent_' + file.id).html(file.percent + "%"); } } }); uploader.init(); } $(document).ready(function(){ $(".uploader").each(function(){ var $obj = $(this); NewFileUploader($obj); }); });
PYTHON
#上傳檔案的處理 @login_required def new_file_upload(R): data = R.POST table_name = data.get('table_name', '') row_id = data.get('row_id', '') f = R.FILES.get('file', None) full_name = f.name.split(".") ext = full_name[-1] full_name.remove(full_name[-1]) name = "".join(full_name) if table_name == 'QuestionFile': new = QuestionFile( name = name, ) if question_id: new.question = Question.objects.get(id=question_id) new.save() getattr(new, 'file').save('%s.%s'%(new.id, ext), f) new.save() return HttpResponse(json.dumps({'status': True, 'id': new.id, 'name': new.name, 'rExt': ext})) #下載檔案專用 @login_required def download_question_file(R, **kw): file_id = kw['file_id'] row = QuestionFile.objects.get(id=file_id) f = open(os.path.join(ROOT, row.file.name), 'rb') content = f.read() response = HttpResponse(mimetype='application/' + row.rExt()) response['Content-Type'] = ('application/' + row.rExt()) file_name = row.name.replace(" ", "") + '.' + row.rExt() response['Content-Disposition'] = ('attachment; filename='+ file_name).encode('cp950') response.write(content) return response
全站熱搜
留言列表