フラスコでファイルをアップロードしようとしていますが、ファイルが正常にアップロードされているようですが、フォームが検証された後、それは私のAjaxの機能には触れませんが、それは私のコードのビットが私のブラウザはちょうどここフラスコでAjaxファイルをアップロード - 成功したブラウザではなくブラウザでjson応答
を示すJSONと白い画面を表示するようだ - AJAX要求 - 使用してjQueryの2.0.3は
var form_data = new FormData($('#firmware_file')[0]);
var url = "{{ url_for('update.update_firmware') }}";
$.ajax({
type: 'POST',
url: url,
data: form_data,
processData: false,
contentType: 'application/json',
cache: false,
async: false,
success: function(data) {
console.log("YES I DO STUFF HERE IT NEVER FIRES");
}
});
フラスコ形状加工
if form2.validate_on_submit():
uploaded_file = request.files[form2.firmware_file.name]
file_data = uploaded_file.read()
return_data = {}
return_data['uploading'] = 'blerg' #uploaded_file.filename
file_path = os.path.join('/tmp', secure_filename(uploaded_file.filename))
open(file_path, 'w').write(file_data)
APP.decoder.firmware_file = file_path
APP.decoder.firmware_length = len(filter(lambda x: x[0] == ':', file_data))
return jsonify(return_data)
「ファイル名」JSON:
は、ここでフォーム定義これはちょうど私の「アップロード」と白い画面を返すように思わ
<form id="uploader_manual" class="form-box " method="POST" action="/update/update_firmware" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="firmware_file">
<label for="firmware_file">Firmware File</label>
</label>
<div class="controls">
<input class="firmware_file" id="firmware_file" name="firmware_file" type="file">
<span class="help-inline"></span>
</div>
</div>
<span id="submit-group" class="control-group">
<span class="form-actions">
<input class="btn btn-primary" id="submit" name="submit" value="Upload" type="submit">
</span>
</span>
</form>
です。
全フラスコビュー:これらのケースのほとんどの
@updater.route('/update_firmware', methods=['GET', 'POST'])
@login_required
@admin_required
def update_firmware():
current_firmware = APP.decoder.device.version_number
all_ok = "true"
form = UpdateFirmwareJSONForm()
form2 = UpdateFirmwareForm()
form2.multipart = True
form.firmware_file_json.choices = []
data = None
try:
response = urllib.urlopen(FIRMWARE_JSON_URL)
except IOError:
flash('Cannot connect to server', 'error')
all_ok = "false"
if all_ok is "true":
data = json.loads(response.read())
counter = 0
for firmware in data['firmware']:
form.firmware_file_json.choices.insert(counter,(firmware['file'], firmware['version']))
counter = counter + 1
if form.validate_on_submit():
file_name = form.firmware_file_json.data
zip, headers = urllib.urlretrieve(file_name)
return_data = {}
with zipfile.ZipFile(zip) as zf:
files = [name for name in zf.namelist() if name.endswith('.hex')]
for filename in files:
file_path = os.path.join('/tmp', secure_filename(filename))
file_data = zf.open(filename, 'r').read()
return_data['uploading'] = filename
if not os.path.isfile(file_path):
open(file_path, 'w').write(file_data)
APP.decoder.firmware_file = file_path
APP.decoder.firmware_length = len(filter(lambda x: x[0] == ':', file_data))
zf.close()
return jsonify(return_data);
if form2.validate_on_submit():
uploaded_file = request.files[form2.firmware_file.name]
file_data = uploaded_file.read()
return_data = {}
return_data['uploading'] = uploaded_file.filename
file_path = os.path.join('/tmp', secure_filename(uploaded_file.filename))
open(file_path, 'w').write(file_data)
APP.decoder.firmware_file = file_path
APP.decoder.firmware_length = len(filter(lambda x: x[0] == ':', file_data))
return jsonify(return_data)
return render_template('updater/firmware_json.html', current_firmware=current_firmware, form=form, form2=form2, firmwarejson=data, all_ok=all_ok);
成功したことはありますが、それは省かれています。実行されることはありません。文字通りこのhttp://imgur.com/a/1lonzにレンダリングされます。 トレースバック?それはJavaScriptとJavaScriptで可能ですか? Firebugはリクエストを表示しますが、表示されている空白のページに「リダイレクト」し、ファンクバグ情報を取得できません。 – weevisss
jsonを返すので、jsonを表示しています。成功関数の働きを調べるには 'console.log( 'something')'を入れます。 – 0decimal0
もちろん私はjsonを返しています - 成功関数は決して発動しません - それはリダイレクトされているように動作します。 http://imgur.com/a/1lonzは私がブラウザで得るものです。文字通り私のテンプレートは消えてしまい、私のjsonレスポンスで同じページの新しいページに移動します。 – weevisss