赞
踩
添加插件的引入,插件遵循官网就好 https://ionicframework.com/docs/v3/native/
- $ ionic cordova plugin add cordova-plugin-filechooser
- $ npm install --save @ionic-native/file-chooser@4
//上传文件
import { File } from '@ionic-native/file';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { FileChooser } from '@ionic-native/file-chooser';
import { FilePath } from '@ionic-native/file-path';
* 调起文件选择器选择文件
*/
chooseFile(){
this.fileChooser.open()
.then(uri => {
console.log(uri);
// 文件虚假地址 无法上传使用 需要转化获取手机真实地址
// alert(uri);
// 根据uri,获取手机端地址
this.filePath.resolveNativePath(uri)
.then(filePath =>{
var fileUrl = filePath;
// 文件真实名称
this.fileName = fileUrl.substr(fileUrl.lastIndexOf('/')+1)
console.log(this.fileName)
this.fileurl = filePath
console.log(filePath)
alert(filePath);
})
.catch(err => console.log(err));
})
.catch(e => {
console.log(e)
// alert(e);
this.fileName = ""
});
}
//上传文件 请求服务器
upLoadFileServe() {
// alert(this.UserList[1]);
//随机图片名称前端
// var date = new Date().getMilliseconds();
const fileTransfer: FileTransferObject = this.transfer.create();
//文件路径
var _fileUrl = this.fileurl;
// alert(_fileUrl);
//上传地址
var api = this.http.apiUrl + "UploadFiles"
//图片参数
let str_para = JSON.stringify({
"Type": "2",
"Number": ""//参数
}) ;
// var PicName = date + "_" + this.fileName;
var PicName = this.fileName1;
let options: FileUploadOptions = {
fileKey: 'imgFile',
fileName: PicName, //上传名称
mimeType: 'image/PNG',//上传类型
params: {
paraJson: str_para,
}
}
//控件上传
fileTransfer.upload(_fileUrl, api, options).then((data) => {
console.dir(data);
alert("上传成功");
// alert(data);
}, (err) => {
console.dir(JSON.stringify(err));
alert("上传失败");
// alert(JSON.stringify(err));
this.fileurl = JSON.stringify(err)
})
}
服务器端(asp.net)
public string UploadFiles()
{
string paraJson = HttpContext.Current.Request["paraJson"];
string json = "";
if (string.IsNullOrWhiteSpace(paraJson))
{
ResultInfo result = new ResultInfo("1", "comMethod", "参数为空");
json = JsonConvert.SerializeObject(result);
return json;
}
//LogHelper.PrintLog("UploadFiles", paraJson);
HttpPostedFile file = HttpContext.Current.Request.Files["imgFile"];
return new WebPhoto().UploadFiles(paraJson, file);
}
//上传附件
public string UploadFiles(string paraJson, HttpPostedFile file)
{
string json = "";
ResultInfo result = null;
if (string.IsNullOrWhiteSpace(paraJson))
{
result = new ResultInfo("1", "UploadFiles", "参数为空");
json = JsonConvert.SerializeObject(result);
return json;
}
JObject jo = (JObject)JsonConvert.DeserializeObject(paraJson);
string type = jo["Type"].ToString();
#region 替换文件
if (type == "2")
{
string number = jo["Number"].ToString();
string direct = "/Upload/FilePack/" + number + "/";
//string direct = "/Upload/FilePack/" + FilePackNum + "/";//pc端保存
string url = wbCommonClass.BASEPATH + direct;
if (!System.IO.Directory.Exists(url))
{
System.IO.Directory.CreateDirectory(url);
}
string savePath = url + Path.GetFileName(file.FileName);
file.SaveAs(savePath);
string strSQL = string.Format(@"Insert into UploadFile(GUIDName,OrigName,Directory,fl_Url,Key1)
VALUES('{0}','{1}','{2}','{3}','{4}')",
file.FileName, file.FileName, direct, "项目文件替换", number);
//pc端保存
// strSQL = string.Format("INSERT INTO UploadFile(GuidName, OrigName, Directory, Description,Key1,MD5) VALUES('{0}','{1}','{2}','{3}','{4}','{5}')",
//FileName, FileName, direct, Description, Key1, md5);
bool b = BP.DA.DBAccess.RunSQL(strSQL) > 0 ? true : false;
if (b)
{
result = new ResultInfo("0", "UploadFiles", "上传成功");
}
else
{
result = new ResultInfo("1", "UploadFiles", "上传失败");
}
json = JsonConvert.SerializeObject(result);
return json;
}
#endregion
else
{
result = new ResultInfo("1", "UploadFiles", "参数为空");
json = JsonConvert.SerializeObject(result);
return json;
}
}
有问题,可留言沟通
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。