android调用本地js文件上传,利用node.js android 实现文件上传

这篇博客介绍了如何使用Node.js搭建一个文件上传服务器,并创建一个简单的Android应用进行文件上传。Node.js部分通过formidable库处理POST请求,保存上传文件,并显示上传结果。Android布局文件中包含了上传按钮,可以调用不同的上传方式。此示例为初学者提供了Web开发与移动开发结合的实际操作案例。
摘要由CSDN通过智能技术生成

1,新建一个node.js项目,并且安装依赖包:formidable

2,app.js文件配置如下:

var http = require('http');

var fs = require('fs');

var formidable = require('formidable');

var firstPage = function(res){

res.writeHead(200, {'Content-Type': 'text/html'});

var html = fs.readFileSync(__dirname + '/public/form.html');

res.end(html);

}

var resultPage = function(res,data,files){

res.setHeader('Content-Type', 'text/html');

res.write('

thanks ' + data.name + '

');

res.write('

  • ');

console.log(data);

console.log(files);

if (Array.isArray(files.images)) {

files.images.forEach(function(image){

var kb = image.size / 1024 | 0;

res.write('

uploaded ' + image.name + ' ' + kb + 'kb');

});

} else {

var image = files.images;

var kb = image.size / 1024 | 0;

res.write('

uploaded ' + image.name + ' ' + kb + 'kb');

}

res.end('

');

}

var server = http.createServer(function(req, res) {

if (req.method == 'GET'){

return firstPage(res);

}

var form = new formidable.IncomingForm;

var data = {};

var files = {};

form.uploadDir = __dirname +'/file';

form.keepExtensions = true;

function ondata(name, val, data){

if (Array.isArray(data[name])) {//数组

data[name].push(val);

} else if (data[name]) {//同key

data[name] = [data[name], val];

} else {//第一次

data[name] = val;

}

}

form.on('field', function(name, val){

ondata(name, val, data);

});

form.on('file', function(name, val){

ondata(name, val, files);

});

form.on('end', function() {

resultPage(res,data,files);

});

form.parse(req);

});

server.listen(8080);

console.log('listening on http://localhost:8080');

2,在public 目录下新建一个form.html文件,文件内容如下:

点击上传就可以在网页上实现上传

3,android布局文件内容如下:

android:id="@+id/RelativeLayout1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/textViewInfo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"/>

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/textViewInfo"

android:layout_marginLeft="40dp"

android:layout_marginTop="20dp"

android:text="文件路径"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/textViewFile"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1"/>

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textViewFile"

android:layout_below="@+id/textViewFile"

android:layout_marginTop="10dp"

android:text="上传网址"

android:textAppearance="?android:attr/textAppearanceMedium" />

android:id="@+id/textViewUrl"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView3"

android:layout_below="@+id/textView3"/>

android:id="@+id/webViewResult"

android:layout_width="fill_parent"

android:layout_height="100dp"

android:layout_below="@+id/textViewUrl"

android:layout_marginTop="30dp"/>

android:id="@+id/buttonJava"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_marginLeft="60dp"

android:text="Java上传" />

android:id="@+id/buttonApache"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/buttonJava"

android:layout_toRightOf="@+id/buttonJava"

android:text="Apache上传" />

即可实现上传功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值