その問題について私を助けてください: nginxなしでフォームを送信する場合
はnginx-upload-moduleとnginx-progress-moduleがあります
:POSTデータとrequest.FILESを取得します-すべての標準
であり、
反対
の問題はありません:
そのフォームを送信する場合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 $"; ValeryKholodkov
に感謝します