Is this the right way to read the content of a file picked by a filepicker? I need to read the image data in order to send it to a webservice in my Windows Metro Javascript App. I use a readFile function with a callback that returns an evt parameter and then use encodeURIComponent(evt.target.result):
document.getElementById("btnUpload").onclick = function () {
var input = document.getElementById("file_input");
readFile(input.files[0], function(file, evt)
{
WinJS.xhr({
type: "post",
url: "http://servlett.domain.com:8080/Servlet/addImage",
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: "fk_floor_id=" + currentFloorId + "&map=" + encodeURIComponent(evt.target.result)
}).then(
function (xhr) {
var success = xhr.response;
}, function (xhr) {
var error = xhr.response;
}
);
});
The parameter evt.target.result is retrieved through the following method:
function readFile(file, callback) {
var reader = new FileReader();
reader.onload = function (evt) {
if (typeof callback == "function")
callback(file, evt);
};
reader.readAsText(file);
}
where file_input is a input component inside the following form:
<form action="" method="post">
<input type="file" id="file_input" />
<button type="button" id="btnUpload">Upload</button>
</form>
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…