Google apps script 上传从昨天开始损坏的二进制文件,解决方法?
更新:我创建了一个问题: 我的web应用程序运行了很长一段时间,但是现在上传的pdf文件突然被破坏了。下面是一个小的简化示例,可用于重现该问题 查看上传的文件内容,看起来文件内容被视为文本,几个字符被替换为Google apps script 上传从昨天开始损坏的二进制文件,解决方法?,google-apps-script,file-upload,web-applications,Google Apps Script,File Upload,Web Applications,更新:我创建了一个问题: 我的web应用程序运行了很长一段时间,但是现在上传的pdf文件突然被破坏了。下面是一个小的简化示例,可用于重现该问题 查看上传的文件内容,看起来文件内容被视为文本,几个字符被替换为EF BF BD,这是“替换字符”(U+FFFD)的UTF-8字节序列 例如,原始PDF文件的第一个字节: 25 50 44 46 2D 31 2E 34 0A 25 E2 E3 CF D3 0A 31 39 |%PDF-1.4\n% 它被上传为: 25 50 44 46 2D 31 2E 3
EF BF BD
,这是“替换字符”(U+FFFD)的UTF-8字节序列
例如,原始PDF文件的第一个字节:
25 50 44 46 2D 31 2E 34 0A 25 E2 E3 CF D3 0A 31 39 |%PDF-1.4\n%
它被上传为:
25 50 44 46 2D 31 2E 34 0A 25 EF BF BD EF BF BD EF BF BD EF BF BD 0A 31 39 |%PDF-1.4\n%
我不知道该在哪里报告,我只希望谷歌的员工能看到并修复它
与此同时,也许熟悉谷歌应用程序脚本的人有了解决办法
下面是一个小的简化示例-部署、上载二进制文件、转到驱动器、在“test”文件夹下找到它、下载它、观察它是否已损坏
HTML模板,文件名test\u form.HTML
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="test-form">
<input type="file" id="test-file" name="test-file">
<button id="submit-button" type="submit">Upload</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function () {
'use strict';
$('#test-form').submit(function (e) {
e.preventDefault(); // prevent form from submitting
google.script.run
.withFailureHandler(fileUploadedFailure)
.withSuccessHandler(fileUploaded)
.uploadFilesFrame(this);
});
function fileUploaded(status) {
alert(status);
}
function fileUploadedFailure(error) {
alert('Failed: ' + error.message);
}
})();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="test-form">
<input type="file" id="test-file" name="test-file">
<input type="hidden" id="test-file2" name="test-file2">
<input type="hidden" id="test-file-name" name="test-file-name">
<button id="submit-button" type="submit">Upload</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function () {
'use strict';
$('#test-form').submit(function (e) {
var thisForm = this;
e.preventDefault(); // prevent form from submitting
var reader = new FileReader();
reader.onload = function (event) {
var result = event.target.result;
var base64 = result.substr(result.indexOf(',') + 1);
$('#test-file2').val(base64);
var filename = $('#test-file').val().split('\\').pop();
$('#test-file-name').val(filename);
$('#test-file').prop('disabled', true);
google.script.run
.withFailureHandler(fileUploadedFailure)
.withSuccessHandler(fileUploaded)
.uploadFilesFrame(thisForm);
};
reader.onerror = function (event) {
alert("ERROR: " + event.target.error.code);
};
reader.readAsDataURL(document.getElementById('test-file').files[0]);
});
function fileUploaded(status) {
alert(status);
}
function fileUploadedFailure(error) {
alert('Failed: ' + error.message);
}
})();
</script>
</body>
</html>
一种解决方法是:在客户端对其进行base64编码,然后在服务器上对其进行base64解码。那里面的东西就没有弄糟。下面是一个例子:
HTML模板,文件名test\u form.HTML
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="test-form">
<input type="file" id="test-file" name="test-file">
<button id="submit-button" type="submit">Upload</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function () {
'use strict';
$('#test-form').submit(function (e) {
e.preventDefault(); // prevent form from submitting
google.script.run
.withFailureHandler(fileUploadedFailure)
.withSuccessHandler(fileUploaded)
.uploadFilesFrame(this);
});
function fileUploaded(status) {
alert(status);
}
function fileUploadedFailure(error) {
alert('Failed: ' + error.message);
}
})();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="test-form">
<input type="file" id="test-file" name="test-file">
<input type="hidden" id="test-file2" name="test-file2">
<input type="hidden" id="test-file-name" name="test-file-name">
<button id="submit-button" type="submit">Upload</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
(function () {
'use strict';
$('#test-form').submit(function (e) {
var thisForm = this;
e.preventDefault(); // prevent form from submitting
var reader = new FileReader();
reader.onload = function (event) {
var result = event.target.result;
var base64 = result.substr(result.indexOf(',') + 1);
$('#test-file2').val(base64);
var filename = $('#test-file').val().split('\\').pop();
$('#test-file-name').val(filename);
$('#test-file').prop('disabled', true);
google.script.run
.withFailureHandler(fileUploadedFailure)
.withSuccessHandler(fileUploaded)
.uploadFilesFrame(thisForm);
};
reader.onerror = function (event) {
alert("ERROR: " + event.target.error.code);
};
reader.readAsDataURL(document.getElementById('test-file').files[0]);
});
function fileUploaded(status) {
alert(status);
}
function fileUploadedFailure(error) {
alert('Failed: ' + error.message);
}
})();
</script>
</body>
</html>
既然您阻止了提交的默认行为,那么这样做怎么样:
事实上,我现在不想告诉你这些,因为我怀疑你比我知道的更多。但是为了帮助别人,可能看起来很愚蠢,我建议你这样做
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
<input type="file" id="test-file" name="test-file">
<input type="button" value="Submit" onClick="this.parentNode" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function uploadFile(form) {
google.script.run
.withFailureHandler(fileUploadedFailure)
.withSuccessHandler(fileUploaded)
.uploadFilesFrame(form);
}
function fileUploaded(status) {
alert(status);
}
function fileUploadedFailure(error) {
alert('Failed: ' + error.message);
}
</script>
</body>
</html>
函数上传文件(表单){
google.script.run
.withFailureHandler(文件上载失败)
.withSuccessHandler(已上载文件)
.上传文件框架(表格);
}
功能文件上传(状态){
警报(状态);
}
函数fileUploadedFailure(错误){
警报('失败:'+错误消息);
}
您可以在issuetracker中报告。有关更多详细信息,请参阅。谢谢,创建了一个问题:请将其添加到您的问题或答案中。