我试图通过一些额外的数据将图片上传到我的服务器。我的angularJs代码就是:
public boolean add(E element)
{
if(root==null)
{
}
else
{
while(true)
{
if(comp < 0)
{
}
else if (temp.right!=null)
{
temp=temp.right;
}
else
{
temp.right=new Entry<E> (element,temp);
}
/**/size++;
/**/return true;
} // bad indentation!
} // bad indentation!
} // bad indentation!
在我的服务器中,我有这样的观点:
size++; return true;
发出请求后,问题已创建,图片文件已上传到Amazon S3。但是,我无法在我的电脑中打开生成的文件。我在哪里做错了?
答案 0 :(得分:2)
经过长时间的互联网搜索后,我找到了答案。首先,我用
读取文件$cordovaFile.readAsArrayBuffer(directory,filename)
之后,我用文件:
创建了一个Blob对象var imgBlob = new Blob([success], { type: "image/jpeg" } );
我的最终angularJS代码是:
function create_question(question, callback){
var form = new FormData()
$cordovaFile.readAsArrayBuffer(cordova.file.dataDirectory, question.name)
.then(function (success) {
var imgBlob = new Blob([success], { type: "image/jpeg" } );
form.append("file", imgBlob)
form.append("title", question.title)
form.append("options", JSON.stringify(question.options))
form.append("correct_option", question.correct_option)
form.append("question_id", question.question_id)
form.append("project_id", question.project_id)
var settings = {
"url": "http://127.0.0.1:8000/" + "question/api/create_question/",
"method": "POST",
"headers": {
'Content-Type': undefined
},
"filename": question.id,
"processData": false,
"data": form,
"file": success,
"filename": "file"
}
$http(settings).then(function (response) {
if (response.data.hasOwnProperty("date_str")) {
callback(true, response.data)
console.log("succesFull")
} else {
console.log(JSON.stringify(response.data))
callback(false, response.data)
}
}, function (response) {
console.log(JSON.stringify(response.data))
callback(false, response.data)
});
// success
}, function (error) {
callback(false,error)
// error
});
}