mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-09-21 08:23:20 +00:00
with open() as 语句添加try&except,返回上传错误
This commit is contained in:
parent
196c3ee934
commit
a084614af9
@ -147,9 +147,12 @@ class TestCaseUploadAPIView(APIView):
|
|||||||
f = request.FILES["file"]
|
f = request.FILES["file"]
|
||||||
|
|
||||||
tmp_zip = "/tmp/" + rand_str() + ".zip"
|
tmp_zip = "/tmp/" + rand_str() + ".zip"
|
||||||
with open(tmp_zip, "wb") as test_case_zip:
|
try:
|
||||||
for chunk in f:
|
with open(tmp_zip, "wb") as test_case_zip:
|
||||||
test_case_zip.write(chunk)
|
for chunk in f:
|
||||||
|
test_case_zip.write(chunk)
|
||||||
|
except IOError:
|
||||||
|
return error_response(u"上传错误,写入临时目录失败")
|
||||||
|
|
||||||
test_case_file = zipfile.ZipFile(tmp_zip, 'r')
|
test_case_file = zipfile.ZipFile(tmp_zip, 'r')
|
||||||
name_list = test_case_file.namelist()
|
name_list = test_case_file.namelist()
|
||||||
|
@ -18,10 +18,16 @@ class SimditorImageUploadAPIView(APIView):
|
|||||||
|
|
||||||
image_name = rand_str() + '.' + str(request.FILES["image"].name.split('.')[-1])
|
image_name = rand_str() + '.' + str(request.FILES["image"].name.split('.')[-1])
|
||||||
image_dir = settings.IMAGE_UPLOAD_DIR + image_name
|
image_dir = settings.IMAGE_UPLOAD_DIR + image_name
|
||||||
with open(image_dir, "wb") as imageFile:
|
try:
|
||||||
for chunk in img:
|
with open(image_dir, "wb") as imageFile:
|
||||||
imageFile.write(chunk)
|
for chunk in img:
|
||||||
|
imageFile.write(chunk)
|
||||||
|
except IOError:
|
||||||
|
return Response(data={
|
||||||
|
"success": True,
|
||||||
|
"msg": "上传错误",
|
||||||
|
"file_path": "/static/upload_image/" + image_name})
|
||||||
return Response(data={
|
return Response(data={
|
||||||
"success": True,
|
"success": True,
|
||||||
"msg": "error message",
|
"msg": "",
|
||||||
"file_path": "/static/upload_image/" + image_name})
|
"file_path": "/static/upload_image/" + image_name})
|
||||||
|
Loading…
Reference in New Issue
Block a user