gpt4 book ai didi

android - 如何使用 phonegap 在 django 服务器中上传文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:38 24 4
gpt4 key购买 nike

这是我的 phonegapfile 文件:该文件正在将数据发送到 django 服务器,但我无法将文件存储在服务器中。python View 函数用于捕获和存储文件是什么?

<!DOCTYPE HTML>
<html>
<head>
<title>File Transfer Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {

// Retrieve image file location from specified source
navigator.camera.getPicture(uploadPhoto,
function(message) {
alert('get picture failed');},
{ quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
);

}

function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://something.com/uploadphonegapfil/"),win,fail,options);
}

function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}

function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Upload File</p>
</body>
</html>

View .py

@csrf_exempt

def uploadPhonegapFile(request):
print "-------- hitting the url"
to_json={}
return HttpResponse(simplejson.dumps(to_json), mimetype= 'application/json')

该上传请求到达服务器,我得到打印“----------------命中 url”那么我怎样才能在这里抓到文件呢?或者有什么方法可以将文件存储在我的服务器文件夹中??

最佳答案

如果您使用 JQuery 通过 POST 将文件发送到服务器,请使用 request.FILES['file'] 代替 request.POST[]

if request.method == 'POST':
filename=request.FILES['file']
form = SomeForm(request.POST, request.FILES)
if form.is_valid():
dest_file = open('C:/system/upload/'+ str(filename), 'wb+')
path = 'C:/system/upload/upload/'+ str(filename)
for chunk in request.FILES['file'].chunks():
dest_file.write(chunk)
dest_file.close()

关于android - 如何使用 phonegap 在 django 服务器中上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com