Javascript 将文件从android上传到应用程序脚本
嗯,所以基本上,我正在尝试将一个文件从我的android手机上传到应用程序脚本,该脚本将在google drive中创建该文件。当通过浏览器访问时,这将起作用。但是当尝试执行multipart/httppost/formdata时,它不起作用。这个参数似乎是正确的。发布字符串没有任何问题(尝试使用volley库),但是文件/图像似乎不起作用。添加Google drive SDK对我来说不是一个选项,因为我的大多数应用程序用户将使用自定义操作系统,因此Google play服务无法正常工作。 应用程序脚本的当前代码 server.gsJavascript 将文件从android上传到应用程序脚本,javascript,android,google-apps-script,Javascript,Android,Google Apps Script,嗯,所以基本上,我正在尝试将一个文件从我的android手机上传到应用程序脚本,该脚本将在google drive中创建该文件。当通过浏览器访问时,这将起作用。但是当尝试执行multipart/httppost/formdata时,它不起作用。这个参数似乎是正确的。发布字符串没有任何问题(尝试使用volley库),但是文件/图像似乎不起作用。添加Google drive SDK对我来说不是一个选项,因为我的大多数应用程序用户将使用自定义操作系统,因此Google play服务无法正常工作。 应用
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
函数上传文件(表单){
试一试{
var dir=“文件”;
var folder,folders=DriveApp.getFoldersByName(dir);
if(folders.hasNext()){
folder=folders.next();
}否则{
folder=DriveApp.createFolder(dir);
}
var blob=form.myFile;
//var file=folder.createFile(“BlobTest”、“test”);
var file=folder.createFile(blob);
file.setDescription(“上传人”+form.myName);
返回文件.getId();
}捕获(错误){
返回错误。toString();
}
}
**Form.html**
功能文件上传(状态){
document.getElementById('myForm').style.display='none';
document.getElementById('output')。innerHTML=status;
}
输入{显示:块;边距:20px;}
**还有我的android代码**
私有类uploadimg扩展异步任务{
int serverResponseCode=0;
字符串服务器响应消息;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的整数doInBackground(Void…params){
返回uploadFilez();
}
public int uploadFilez(){
字符串sourceFileUri=selectedImagePath;
字符串upLoadServerUri=url;
字符串fileName=sourceFileUri;
HttpURLConnection conn=null;
DataOutputStream dos=null;
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
int字节读取,字节可用,缓冲区大小;
字节[]缓冲区;
int maxBufferSize=1*1024*1024;
File sourceFile=新文件(sourceFileUri);
如果(!sourceFile.isFile()){
返回0;
}
尝试{//打开到Servlet的URL连接
FileInputStream FileInputStream=新的FileInputStream(sourceFile);
URL URL=新URL(upLoadServerUri);
conn=(HttpURLConnection)url.openConnection();//打开到url的HTTP连接
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不要使用缓存副本
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“ENCTYPE”、“多部分/表单数据”);
conn.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
conn.setRequestProperty(“myFile”,文件名);
//conn.setRequestProperty(“接受字符集”、“UTF-8”);
//字符串k=“”;
dos=新的DataOutputStream(conn.getOutputStream());
写字节(两个连字符+边界+行结束);
dos.writeBytes(“内容处理:表单数据;name=“+”tits“+”;filename=\”“+filename+”\”“+lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable=fileInputStream.available();//创建最大大小的缓冲区
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
//读取文件并将其写入表单。。。
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
而(字节读取>0){
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
}
//发送文件数据后所需的多部分表单数据。。。
dos.writeBytes(lineEnd);
写字节(两个连字符+边界+两个连字符+行结束);
//来自服务器的响应(代码和消息)
serverResponseCode=conn.getResponseCode();
serverResponseMessage=conn.getResponseMessage();
如果(服务器响应代码==200){
runOnUiThread(新的Runnable(){
公开募捐{
}
});
}
//关闭溪流//
fileInputStream.close();
dos.flush();
dos.close();
}捕获(格式错误){
例如printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
dialog.dismise();
返回服务器响应代码;
}
@凌驾
受保护的void onPostExecute(整数结果){
super.onPostExecute(结果);
}
}
感谢您的帮助:)谢谢这对您有帮助吗?不太好,不过还是谢谢你:)
function uploadFiles(form) {
try {
var dir = "Files";
var folder, folders = DriveApp.getFoldersByName(dir);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dir);
}
var blob = form.myFile;
// var file = folder.createFile("BlobTest","test");
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.myName);
return file.getId() ;
} catch (error) {
return error.toString();
}
}
**Form.html**
<form id="myForm">
<input type="text" name="myName" placeholder="name..">
<input type="file" name="myFile" >
<input type="submit" value="Upload File"
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
<div id="output"></div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
**And my android code**
private class uploadimg extends AsyncTask<Void, Integer, Integer> {
int serverResponseCode = 0;
String serverResponseMessage;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
return uploadFilez();
}
public int uploadFilez() {
String sourceFileUri = selectedImagePath;
String upLoadServerUri = url;
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
// conn.setRequestProperty("Accept-Charset", "UTF-8");
// String k ="";
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="+"tits"+";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
serverResponseMessage = conn.getResponseMessage();
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
return serverResponseCode;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
}
}