I have written a POST api which posts a file. I want to open the file and parse a JSON content in the file. But the line os.path.join() doesnt seem to work. I am getting "'InMemoryUploadedFile' object has no attribute 'startswith'" exception. Have included the code used below.
def schedule_load(file_name):
print file_name
file_json = default_storage.open(os.path.join("schedule_files", file_name), 'r')
var = file_json.read()
print var
file_json.close()
schedule = json.loads(var)
My POST method :
@api_view(['POST'])
def post_schedule(request):
print "post_schedule"
if request.method == 'POST':
print "if POST"
form = ScheduleForm(request.POST, request.FILES)
file_name = form['schedule_file'].value()
if form.is_valid():
print "is_valid"
form.save()
schedule_load(file_name)
return JsonResponse({"status": "success"}, status=status.HTTP_200_OK)
else:
return JsonResponse({"status": "Invalid file"}, status=status.HTTP_200_OK)
Because what you are dealing with is a InMemoryUploadedFile
rather than a file name. Your code should change as
def schedule_load(file_name):
var = file_name.read()
print var