1,新建一個node.js項目,并且安裝依賴包:formidable
2,app.js文件配置如下:
var http = require('http');
var fs = require('fs');
var formidable = require('formidable');
nodejs做后端好嗎?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){
android上傳文件到服務器、res.setHeader('Content-Type', 'text/html');
res.write('
thanks ' + data.name + '
');res.write('
console.log(data);
console.log(files);
node運行js文件、if (Array.isArray(files.images)) {
files.images.forEach(function(image){
var kb = image.size / 1024 | 0;
res.write('
uploaded ' + image.name + ' ' + kb + 'kb');});
} else {
androidstudio導入jar包。var image = files.images;
var kb = image.size / 1024 | 0;
res.write('
uploaded ' + image.name + ' ' + kb + 'kb');}
res.end('
');}
js調用安卓原生方法。var server = http.createServer(function(req, res) {
if (req.method == 'GET'){
return firstPage(res);
}
var form = new formidable.IncomingForm;
var data = {};
node調用網頁?var files = {};
form.uploadDir = __dirname +'/file';
form.keepExtensions = true;
function ondata(name, val, data){
if (Array.isArray(data[name])) {//數組
data[name].push(val);
node調用java sdk,} else if (data[name]) {//同key
data[name] = [data[name], val];
} else {//第一次
data[name] = val;
}
}
nodejs調用python函數。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上傳" />
即可實現上傳功能版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态