Cannot use "image.save" on django
My error is:
IOError at /mytest/photo/upload/
[Errno 2] No such file or directory: u'/mytest/photo/upload/2.png'
And my view is:
UPLOAD_URL = '/mytest/photo/upload/'
def upload(request):
buf = request.FILES.get('photo', None)
print buf
if buf:
#data = buf.read()
#f = StringIO.StringIO(data)
image = Image.open(buf)
#image = image.conver开发者_JAVA百科t('RGB')
name = '%s%s' % (UPLOAD_URL, buf.name)
image.save(file(name, 'wb'), 'PNG')
return HttpResponse('ok')
return HttpResponse('no')
And my urls.py is:
urlpatterns = patterns('mytest.views',
url(r'^photo/upload/$','upload',name="")
)
How can I fix this?
You have to specify the local path where you want to save the file. UPLOAD_URL is the path to your view, not the directory.
name a variable UPLOAD_PATH with full path for upload
then do something like :
UPLOAD_PATH = '/path/to/uploads/user'
if not os.path.isdir(UPLOAD_PATH):
os.mkdir(UPLOAD_PATH)
file_path = os.path.join(UPLOAD_PATH, filename)
# security check
if os.path.abspath(file_path ).startswith(UPLOAD_PATH):
image.save(file(file_path , 'wb'), 'PNG')
精彩评论