Django Python将文件上载到特定的文件夹

时间:2021-10-09 06:53:21

I am working on a Django app and I want the users to be able to upload a csv file to a folder in the server. Appreciate if you could point me to the right direction.

我正在开发Django应用程序,我希望用户能够将csv文件上载到服务器中的文件夹。如果你能给我指出正确的方向,我将不胜感激。

3 个解决方案

#1


1  

You can just use the django FileField and that lets you upload the file to the specified folder directly through admin panel and same can be done using the form for normal user.

您可以使用django FileField,它允许您直接通过管理面板将文件上载到指定的文件夹中,也可以使用普通用户的表单来完成同样的操作。

upload = models.FileField(upload_to='uploads/your-folder/')

#2


0  

Clients can send files via multi-part requests. Following code demonstrates multi part requests using requests

客户端可以通过多部分请求发送文件。下面的代码演示了使用请求的多部分请求

import requests
url = "http://someurl"
files = {"file" : ("somename.txt", open("pathtofile", "rb"))}
requests.post(url, files=files)

This will give you an InMemoryFile in your server, which you can then save to your server using default_storage and ContentFile inbuilt in django

这将在您的服务器中提供一个InMemoryFile,然后您可以使用在django中内置的default_storage和ContentFile将其保存到服务器

def filehandler(request):
    incomingfile = request.FILES['file']
    default_storage.save("pathtofolder", ContentFile(incomingfile.read()))

#3


0  

Here is my complete solution

这是我的完整解决方案。

#view.py
def uploadfunc(request):
	if request.method=='POST':
		form =uploadfileform(request.POST,request.FILES)
		if form.is_valid():
			form.save()
			return render_to_response('upload_successful.html')
	else:
		form=uploadfileform()
	return render(request, 'upload.html',{'form':form})


#models.py
class uploadfolder(models.Model):
    """ my application """
    File_to_upload = models.FileField(upload_to='')


#forms.py
#uploading file form
class uploadfileform(forms.ModelForm):
	class Meta:
		model=uploadfolder
		fields=('File_to_upload',)


#upload.html
  <form method="post" action="{% url 'uploadfunc'%}" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_table }}
  <!--  <button type="submit">Upload</button>-->
    <input class="btn btn-primary btn-md" role="button" type="submit" name="submit" value="Upload File" >
  </form>


#settings.py 

MEDIA_ROOT = "/var/www/vmachines/registry/files/"
MEDIA_URL = "files/"

#1


1  

You can just use the django FileField and that lets you upload the file to the specified folder directly through admin panel and same can be done using the form for normal user.

您可以使用django FileField,它允许您直接通过管理面板将文件上载到指定的文件夹中,也可以使用普通用户的表单来完成同样的操作。

upload = models.FileField(upload_to='uploads/your-folder/')

#2


0  

Clients can send files via multi-part requests. Following code demonstrates multi part requests using requests

客户端可以通过多部分请求发送文件。下面的代码演示了使用请求的多部分请求

import requests
url = "http://someurl"
files = {"file" : ("somename.txt", open("pathtofile", "rb"))}
requests.post(url, files=files)

This will give you an InMemoryFile in your server, which you can then save to your server using default_storage and ContentFile inbuilt in django

这将在您的服务器中提供一个InMemoryFile,然后您可以使用在django中内置的default_storage和ContentFile将其保存到服务器

def filehandler(request):
    incomingfile = request.FILES['file']
    default_storage.save("pathtofolder", ContentFile(incomingfile.read()))

#3


0  

Here is my complete solution

这是我的完整解决方案。

#view.py
def uploadfunc(request):
	if request.method=='POST':
		form =uploadfileform(request.POST,request.FILES)
		if form.is_valid():
			form.save()
			return render_to_response('upload_successful.html')
	else:
		form=uploadfileform()
	return render(request, 'upload.html',{'form':form})


#models.py
class uploadfolder(models.Model):
    """ my application """
    File_to_upload = models.FileField(upload_to='')


#forms.py
#uploading file form
class uploadfileform(forms.ModelForm):
	class Meta:
		model=uploadfolder
		fields=('File_to_upload',)


#upload.html
  <form method="post" action="{% url 'uploadfunc'%}" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_table }}
  <!--  <button type="submit">Upload</button>-->
    <input class="btn btn-primary btn-md" role="button" type="submit" name="submit" value="Upload File" >
  </form>


#settings.py 

MEDIA_ROOT = "/var/www/vmachines/registry/files/"
MEDIA_URL = "files/"