【发布时间】:2019-07-20 18:20:32
【问题描述】:
我正在尝试将文件上传到在 chrome 浏览器中运行良好但在 android webview 中运行良好的链接(共享点休息服务)。
我知道 android webview 有很多限制,但我正在尝试使用以下方法上传文件
https://github.com/mgks/Os-FileUp
它打开了浏览器,但它不显示文件名,也不上传任何内容或调用服务,但相同的代码在 chrome 浏览器中工作得很好(请注意我已经启用了 CORS 来测试它
下面是我的html和js代码
我已经成功构建了 android 项目并将我的 html 文件作为 apk 运行。其他带有 get 和 post 的 ajax 代码工作得很好。只有文件上传是个问题
请帮我解决这个文件上传问题,并提供一些可行的示例。也在样本中
<!DOCTYPE html>
<html>
<body>
<h1>Sharepoint File upload</h1>
<form method="POST" enctype="multipart/form-data" id="fileUploadForm">
<!--<input type="text" name="extraField" />--><br /><br />
<input type="file" id="fileupload" name="files" /><br /><br />
<input type="submit" value="Submit" id="btnSubmit" />
</form>
<h1></h1>
<span id="result"></span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#btnSubmit").click(function (event) {
//stop submit the form, we will post it manually.
event.preventDefault();
// Get form
// var form = $('#fileUploadForm')[0];
// Create an FormData object
var data = new FormData();
data.append("File", $('#fileupload')[0].files[0]);
// If you want to add an extra field for the FormData
// data.append("File", form);
console.log('file', $('#fileupload')[0].files[0]);
// disabled the submit button
$("#btnSubmit").prop("disabled", true);
var _url = "https://my.sharepoint.com/teams/<MyProj>/_api/web/getfolderbyserverrelativeurl(\'/teams/<MyProj>/Project Documents/1264\')/files/add(overwrite=true,url=\'TestUpload1.txt\')"
$.ajax({
type: "POST",
// enctype: 'multipart/form-data',
headers: {
'Accept': 'application/json;odata=verbose',
'processData': false,
'Authorization': 'Bearer <key-value>',
'Content-Type': 'application/json'
},
url: _url,
data: data,
processData: false,
contentType: true,
cache: false,
timeout: 600000,
success: function (data) {
$("#result").text(data);
console.log("SUCCESS : ", data);
$("#btnSubmit").prop("disabled", false);
},
error: function (e) {
$("#result").text(e.responseText);
console.log("ERROR : ", e);
$("#btnSubmit").prop("disabled", false);
}
});
});
});
</script>
</body>
</html>
android 代码文件类型仅限于 image/* 但我需要上传 pdf、doc 和 txt 文件
【问题讨论】:
-
第一个链接没有接受的答案,第二个链接有一个接受的答案,但它指的是第一个链接!我玩了很多,仍然无法正常工作。它在笔记本电脑的 chrome 浏览器中就像一个魅力!要求用户提供任何故障排除想法,这将使它起作用!我还在继续努力
标签: android jquery file-upload webview