I am trying to do a file upload to a link (share point rest service) which works well in chrome browser, but not in android webview.
I know android webview comes with a lot of restrictions, but I am trying to upload a file using the following approach
https://github.com/mgks/Os-FileUp
it opens up the browser on but it does not show the file name nor it upload any content or call the service, but the same code works just fine in chrome browser (pls note that I have enabled CORS to test it
my html and js code below
i have successfully built the android project and put my html file and run it as an apk. other ajax codes with get and post are working just fine. only file upload is a problem
please help me with this file upload thing, with some workable example. Also in the sample
<!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 code the filetype is restricted to image/* but i need to upload pdf, doc and txt files