ここに記載されているように、カスタム関数を使用してファイルをチャンクに分割してアップロードします。私の問題は、ファイルを 2 回アップロードしsave()
た後に呼び出すhandle_uploaded_file()
ことです。1 つは "MEDIA_URL/my_path" ディレクトリに、もう 1 つは "MEDIA_URL" に。しかし、チャンクを含むアップロードは 1 つだけにしたいと思います。save()
「チャンク」アップロードを強制することは可能ですか? それとも、別のアプローチを使用する必要がありますか? ありがとうございました。
models.py
class ShapeFile(models.Model): name = models.CharField(max_length=100) srid = models.ForeignKey(SpatialRefSys) user = models.ForeignKey(User) color_table = models.ForeignKey(ColorTable) file = models.FileField(upload_to="my_path") class Meta: unique_together = ('name', 'user')
フォーム.py
class UploadForm(ModelForm): class Meta: model = ShapeFile fields = ('name','user','srid','file','color_table') widgets = {'srid': TextInput(), 'user': HiddenInput()
ビュー.py
def handle_uploaded_file(fileName, filePath): with open(filePath, 'wb+') as destination: for chunk in fileName.chunks(): destination.write(chunk) @login_required def shapeIng(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): req = request.POST # Split uploaded file into chunks fileName = request.FILES['file'] filePath = ShapeFile(file=fileName).file.path handle_uploaded_file(fileName, filePath) form.save() messages.success(request, 'Shapefile upload succesful!') return redirect('shapeCreated') else: messages.error(request, 'Something went wrong uploading Shapefile.') else: # request.method == 'GET' form = UploadForm(initial={'user': request.user}) return render_to_response('my_app/base_shapeIngestion.html', {'form': form}, context_instance=RequestContext(request))