Fileupload 在 pc 浏览器中有效,但在 android webview 中无效
Fileupload works in pc browser, but not in android webview
我正在尝试将文件上传到 link(共享点休息服务),它在 chrome 浏览器中运行良好,但在 android 网络视图中运行良好。
我知道 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 代码文件类型仅限于图像/* 但我需要上传 pdf、doc 和 txt 文件
将所有文件类型的 image/*
更改为 */*
,或者您可以使用 application/pdf
专门针对 PDF 文件,在 this line.
关于文件上传,一切似乎都很好,应该可以。您是否使用最新的 Os-FileUp update? The repo has just been recently updated with many bug fixes. OR try Smart WebView,如果它适合您。
我正在尝试将文件上传到 link(共享点休息服务),它在 chrome 浏览器中运行良好,但在 android 网络视图中运行良好。
我知道 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 代码文件类型仅限于图像/* 但我需要上传 pdf、doc 和 txt 文件
将所有文件类型的 image/*
更改为 */*
,或者您可以使用 application/pdf
专门针对 PDF 文件,在 this line.
关于文件上传,一切似乎都很好,应该可以。您是否使用最新的 Os-FileUp update? The repo has just been recently updated with many bug fixes. OR try Smart WebView,如果它适合您。