죄송합니다, 초보자 용 질문 : 웹 브라우저에서 내 서버로 파일을 업로드 할 수있는 장고 응용 프로그램에 아주 간단한 기능이 있습니다 (완벽하게 작동합니다!). 이제는 웹 브라우저 대신 iPhone을 사용하고 싶습니다.iPhone에서 Django로 파일 업로드
Django에 올바른 양식을 제공하는 방법을 모르는 상태에서 약간의 문제가 발생했습니다. 즉, 파일 이름과 enctype = "multipart/form-data"가 필요한 것입니다. 여기
장고 내 업로드 기능입니다 :class UploadFileForm(forms.Form):
file = forms.FileField()
def handle_uploaded_file(f):
destination = open('uploads/example.txt', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
print form
print request.FILES
return HttpResponse('Upload Successful')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
내 템플릿이 (하고 Upload.html)과 같습니다
이제<form action="" method="post" enctype="multipart/form-data">
{{ form.file }}
{{ form.non_field_errors }}
<input type="submit" value="Upload" />
</form>
,이 전 간단한 txt 파일을 보내려고한다고 가정하자 내 아이폰 앱에서 서버로. 장고 이것은
입니다 읽을 수
이- 는
- 가
enctype
및 - 는 형식으로 그것의 있는지 확인 지정한 파일 이름을 제공합니다 정말 방법 모른다 내가 얻은 정도 :
NSString *fileContents = [self loadTXTFromDisk:@"example.txt"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://127.0.0.1:8000/uploadfile/"]]; [request setHTTPMethod:@"POST"]; [request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; [request setValue:[NSString stringWithFormat:@"%d", [fileContents length]] forHTTPHeaderField:@"Content-length"]; [request setHTTPBody:[fileContents dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
그러나 Django는 예상 한 형태가 유효하지 않기 때문에 이것을 제외하고는하지 않습니다. Cf. 위 : HTML 양식에서
감사하지만 여전히 작동하지 않습니다. 이제 예외가 발생합니다 : "멀티 파트에서 유효하지 않은 경계 : 없음"경계 란 무엇이며 어떻게 설정해야합니까? 당신의 도움을 주셔서 감사합니다! –
NSString과 같은 것 * boundary = [NSString stringWithString : @ "--------------------------- 경계선 -------- ------------------- "]; NSString * contentType = [NSString stringWithFormat : @ "다중 파트/양식 데이터, 경계 = % @", 경계]; [addValue 요청 : contentType forHTTPHeaderField : @ "Content-Type"]; –
그렇다면 경계선이 무엇인지 생각했습니다. 그것은 무작위인가? –