请帮我解决这个问题:如果我在没有 nginx 的情况下发送表单,
我有 nginx-upload-module 和 nginx-progress-module
:我得到 POST 数据和 request.FILES -all norm
,
相反
我没有问题:
如果我发送该表单使用 nginx
我得到文件的数据(她的大小,名字!!但我没有我的表单数据)
我的代码:
#url.py
urlpatterns = patterns('apps.test_doc.views',
url(r'^test$','initial', name='initial'),
url(r'^test/upload/$','upload', name="upload"),
)
#js
<form id="upload" method="post" enctype="multipart/form-data" onsubmit="progress();">
document.getElementById('upload').action = "/upload/share?X-Progress-ID=" + id;;
#view.py
@csrf_exempt
def initial(request):
data = {
'form': UploadForm(),
}
return render_to_response('upload.html', data, RequestContext(request))
def upload(request):
if request.method == 'POST':
pathfile = request.POST.get('file.path')
upfile = os.path.basename(pathfile)
form = UploadForm(request.POST,initial={'file':upfile})
if form.is_valid():
cd = form.cleaned_data
instance = form.save(commit=False)
instance.save()
return HttpResponseRedirect(reverse('initial'))
#nginx.conf
location = /upload/share {
upload_pass /test/upload/;
client_max_body_size 250m;
upload_store /tmp;
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
upload_cleanup 400 404 499 500-505;
upload_limit_rate 16k;
track_uploads upload 1m;
}
location = /upload/status {
report_uploads upload;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 90s;
proxy_read_timeout 30s;
proxy_pass http://localhost:8152/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
track_uploads upload 30s;
}
我在哪里发现这个问题: Nginx、Django、JS?
添加(其余html):
<form id="upload_form" method="post" enctype="multipart/form-data" accept-charset="utf-8" class="steps" onsubmit="progress();">{% csrf_token %}
<h3>Upload a new file</h3>
{{form.as_p}}
<input type="submit" value="Upload" id="submit" /></p>
</form>
<div id="status" style="display: none;">
<table width="100%">
<tr><th></th><th>load</th><th>rest</th><th>all</th></tr>
<tr><td>time:</td><td id="elapsed">∞</td><td id="remaining">∞</td><td id="total">∞</td></tr>
<tr><td>size:</td><td id="sent">0 b</td><td id="offset">0 b</td><td id="length">0 b</td></tr>
<tr><td>speed:</td><td id="speed">n/a</td></tr>
</table>
<div style="border: 1px solid #c0c0c0;">
<div style="background: #c0c0c0; width: 0%; text-align: right;" id="bar">0%</div>
</div>
#js(全部)
<script type="text/javascript" charset="utf-8">
function progress() {
var ms = new Date().getTime() / 1000;
rq = 0;
id = "";
for (i = 0; i < 32; i++) {
id += Math.floor(Math.random() * 16).toString(16);
}
document.getElementById('upload_form').action = "/upload/share?X-Progress-ID=" + id;;
document.getElementById('status').style.display = 'block'
interval = window.setInterval(function () { fetch(id, ms); }, 1000);
return true;
}
function fetch(id, ms) {
var fetch = new XMLHttpRequest();
fetch.open("GET", "/upload/status", 1);
fetch.setRequestHeader("X-Progress-ID", id);
fetch.onreadystatechange = function () {
if (fetch.readyState == 4) {
if (fetch.status == 200) {
var now = new Date().getTime() / 1000;
var upload = eval(fetch.responseText);
if (upload.state == 'uploading') {
var diff = upload.size - upload.received;
var rate = upload.received / upload.size;
var elapsed = now - ms;
var speed = upload.received - rq; rq = upload.received;
var remaining = (upload.size - upload.received) / speed;
var uReceived = parseInt(upload.received) + ' bytes';
var uDiff = parseInt(diff) + ' bytes';
var tTotal = parseInt(elapsed + remaining) + ' secs';
var tElapsed = parseInt(elapsed) + ' secs';
var tRemaining = parseInt(remaining) + ' secs';
var percent = Math.round(100*rate) + '%';
var uSpeed = speed + ' bytes/sec';
document.getElementById('length').firstChild.nodeValue = parseInt(upload.size) + ' bytes';
document.getElementById('sent').firstChild.nodeValue = uReceived;
document.getElementById('offset').firstChild.nodeValue = uDiff;
document.getElementById('total').firstChild.nodeValue = tTotal;
document.getElementById('elapsed').firstChild.nodeValue = tElapsed;
document.getElementById('remaining').firstChild.nodeValue = tRemaining;
document.getElementById('speed').firstChild.nodeValue = uSpeed;
document.getElementById('bar').firstChild.nodeValue = percent;
document.getElementById('bar').style.width = percent
}
else {
window.clearTimeout(interval);
}
}
}
}
fetch.send(null);
}
</script>
我需要保存模型数据表格和文件路径
我写信给 创建者 nginx-upload-midule解决这个问题
的问题简单需要添加位置指令:
upload_pass_form_field
示例:upload_pass_form_field "^my_name_field$|^description$";
感谢Valery Kholodkov