これは、PythonでDjangoフレームワークを使用して行うことができます。
まずFileField
およびその他のパラメータのためのフィールドを含むフォームを作成します。
from django import forms
class UploadFileForm(forms.Form):
my_parameter = forms.CharField(max_length=50)
file = forms.FileField()
はあなたのビューでUploadFileForm
を含めると、アップロードされたファイルの取り扱いのために、あなたの関数を呼び出す:
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
my_parameter = form.cleaned_data['my_parameter']
# Handle the uploaded file
results = handle_uploaded_file(request.FILES['file'], title)
# Clear the form and parse the results
form = UploadFileForm()
return render(request, 'upload.html', {'form': form, 'results': results})
else:
form = UploadFileForm()
return render(request, 'upload.html', {'form': form})
作成しますアップロードされたファイルを処理し、bashスクリプトを呼び出す関数:
import subprocess
import os
def handle_uploaded_file(f, my_parameter):
file_path = os.path.join('/path/to/destination/', f.name)
# Save the file
with open(file_path, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
# Call your bash script with the
output = subprocess.check_output(['./my_script.sh',str(file_path),str(my_parameter)], shell=True)
return output
ハンドルファイルがDjangoでどのようにアップロードされるかの詳細と手順については、https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/を参照してください。
ようこそstackoverflow。これまであなたは何を裂いたのですか?他人からの回答を得る前に、自分で努力する必要があります。 – tfv