kha*_*iuk 6 django jquery django-rest-framework
我正在尝试使用DRF和jQuery构建文件上传.我用谷歌搜索并找到了这个样本.我在beckand上有一些类似的代码:
class Attachment(BaseModel):
file = models.FileField(upload_to=get_photo_path)
class AttachmentSerializer(serializers.ModelSerializer):
class Meta:
model = models.Attachment
fields = ('id', 'file')
class AttachmentViewSet(viewsets.ModelViewSet):
parser_classes = (FileUploadParser, )
serializer_class = serializers.AttachmentSerializer
queryset = models.Attachment.objects.all()
def pre_save(self, obj):
obj.file = self.request.FILES.get('file')
Run Code Online (Sandbox Code Playgroud)
并尝试将Angular示例转换为jQuery
var fd = new FormData()
fd.append('file', file) // file from file-field
var reader = new FileReader()
$.ajax({
url: 'http://localhost:8001/files/',
data: fd,
processData: false,
contentType: false,
type: 'POST'
}).done(...
Run Code Online (Sandbox Code Playgroud)
出于某些原因,我在尝试上传文件时后端出错:
detail: "FileUpload parse error - none of upload handlers can handle the stream"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2151 次 |
| 最近记录: |