A NativeScript plugin to post/upload file as multipart/form-data to server for iOS and Android. NS 6.1.0+ is required. Please use older version if you have older version of NS platform installed.
[3.0.0]Upgraded to NS 8.0.0. Thanks to Fr3nky88
[2.1.0]Upgraded to NS 6.3.0. Fixed Kotlin issue. More here
[2.0.0]Upgraded to NS 6.2.0. Fixed Kotlin issue. More here
[1.6.0] Added common response in iOS and Android instead returned by native APIs.Thanks to virtualbjorn
[1.5.0] Now supports custom headers
tns plugin add nativescript-http-formdata
Android | iOS |
---|---|
okhttp3 | OMGHTTPURLRQ |
import { TNSHttpFormData, TNSHttpFormDataParam, TNSHttpFormDataResponse } from 'nativescript-http-formdata';
use the ImagePicker plugin or any other.https://github.com/NativeScript/nativescript-imagepicker
private test() {
let context = imagepicker.create({
mode: "single" // use "multiple" for multiple selection
});
context.authorize()
.then(function() {
return context.present();
})
.then((selection) => {
let item = selection[0];
//UIImage for iOS and android.graphics.Bitmap for Android
item.getImageAsync(async (image, error) => {
let fd = new TNSHttpFormData();
//create params. You can upload an array of params i.e multiple data. For every parameter you need to give unique name
//so you can get it on server. Check below how to grab it in ASP.Net MVC
let params = [];
let imageData: any;
if(!image) {
throw 'Could not get image';
}
if(image.ios) {
imageData = UIImagePNGRepresentation(image);
} else {
//can be one of these overloads https://square.github.io/okhttp/3.x/okhttp/okhttp3/RequestBody.html
let bitmapImage: android.graphics.Bitmap = image;
let stream = new java.io.ByteArrayOutputStream();
bitmapImage.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, stream);
let byteArray = stream.toByteArray();
bitmapImage.recycle();
imageData = byteArray;
}
let param: TNSHttpFormDataParam = {
data: imageData,
contentType: 'image/png',
fileName: 'test.png',
parameterName: 'file1'
};
params.push(param);
let param2: TNSHttpFormDataParam = {
data: "John Doe",
parameterName: "firstName"
};
params.push(param2);
console.log('submitting', params);
try {
const response: TNSHttpFormDataResponse = await fd.post('http://10.10.10.149:10025/home/fileupload', params, {
headers: {
test1: "test1 value",
"x-version-no": "2.0"
}
});
console.log(response);
} catch (e) {
console.log('---------------home.component.ts---------------');
console.log(e);
}
});
}).catch(function (e) {
console.log('-------------------error----------------')
console.log(e);
});
}
Now on server to grab the file(s) in ASP.Net MVC, you can follow https://stackoverflow.com/a/16256106/859968 or following
[HttpPost]
//file1 and file2 are parameters name as given in NativeScript object. They must match
public ActionResult FileUpload(HttpPostedFileBase file1, HttpPostedFileBase file2, string firstName)
{
//grab your headers
var headers = Request.Headers;
if (file1 != null)
{
string pic = System.IO.Path.GetFileName(file1.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), pic);
// file is uploaded
file1.SaveAs(path);
}
if (file2 != null)
{
string pic = System.IO.Path.GetFileName(file2.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), pic);
// file is uploaded
file2.SaveAs(path);
}
// after successfully uploading redirect the user
return RedirectToAction("Index", "Home");
}
A donation will not make me rich, but your appreciation makes me happy
/** FormData ==>表单数据 能自动把表单数据拼接打包 当ajax发送数据时,发送打包的数据; 还可以使用FormData对象的append(key,value)添加数据 FormData还可以自动帮忙打包文传送件数据,后台通过$_FILES数组接收 说明FormData对象既可以打包发送表单的数据,也可以手动append数据 注意,input Dom对象有一个files属性(
var utils = require('./utils'); var isBrowser = typeof document !== "undefined"; class Fly { constructor(engine) { this.engine = engine || XMLHttpRequest; this.interceptors = {
nativescript-http �� The best way to do HTTP requests in NativeScript �� A drop-in replacement for the core HTTP with important improvements and additions like proper connection pooling, form data sup
我正在尝试从nativescript向常规http(非https)服务器发出http请求。 当向https服务器发出请求时,该请求工作得很好,但是当向超文本传输协议服务器(这是我目前运行的服务器)发出请求时,它就不工作了。 在iOS中,我必须在信息中添加以下内容。plist文件,以允许向http服务器发送请求,然后该文件正常工作: 然而,在Android中,请求仍然不起作用,并且我找不到任何关于我
NativeScript 可以使用 Javascript,CSS, XML 创建真正的 Native 跨平台应用,支持 iOS Android,NativeScript 将您的跨平台代码翻译成目标平台的代码。 UI 使用 XML 描述,CSS 样式,在编译时将 UI 转化成本地原生代码,最终得到正在的 Native 原生应用。 Telerik 公开了用于创建安卓、iOS和Windows Unive
“OriginalStack”:“错误:java.io.ioException:不允许到本地主机的明文HTTP通信\n位于新区域AwareError(文件:///data/data/org.nativescript.preview/files/app/Tns_modules/@nativescript/Angular/zone-js/dist/zone-nativescript.js:1298:3
NativeScript Command-Line Interface The NativeScript CLI lets you create, build, and deploy NativeScript-based apps on iOS and Android devices. Get it using: npm install -g nativescript What is Native
NativeScript-Snackbar �� �� �� NativeScript plugin for Material Design SnackBar component. Installation: NativeScript 7+:tns plugin add @nstudio/nativescript-snackbar NativeScript version prior to 7:t