webapi批量上传照片到ftp服务器(base64)
2021-04-22 05:27
标签:ddn static tostring info 格式化 methods list mba his 一段demo。做的时候根据自己的需求改造下 webapi批量上传照片到ftp服务器(base64) 标签:ddn static tostring info 格式化 methods list mba his 原文地址:http://www.cnblogs.com/hegx/p/8027033.htmlpublic HttpResponseMessage AddVisitNew(HttpRequestMessage request, CustomerVisitAddNewModel model)
{
return CreateHttpPostResponselong>(request, () =>
{
var res = customerVisitInfoService.AddVisitNew(model, CurrentUserId);
if (string.IsNullOrEmpty(res.Error))
{
// 上传拜访记录照片
string visitPath = null;
FtpClient ftpClient = new FtpClient(GlobalConst.FtpAddress, GlobalConst.Username, GlobalConst.Password);
string visitUploadDir = Path.Combine(GlobalConst.FILE_VISIT, "file");
for (var i = 0; i )
{
//获取base64编码的图片
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];
//获取文件储存路径
string path = context.Request.MapPath("~/"); //获取当前项目所在目录
string fName = "{0}".Fmt(DateTime.Now.ToString("yyyyMMddHHmmssfff")) + i;
string suffix = ".jpg";
string strPath = path + "App_Data/" + fName + suffix;
string newFileName = fName + suffix;
//获取图片并保存
Base64ToImg(model.VisitPic[i].Split(‘,‘)[1]).Save(strPath);
//上传FTP
string uploadFilePath = Path.Combine(visitUploadDir, newFileName);
visitPath = ftpClient.UploadFile(strPath, uploadFilePath, true);
//FTP 转 HTTP
visitPath = "{0}".Fmt(UriHelper.GetAbsolutePath(visitPath));
var pic = new VisitPiction
{
CustomerVisitInfoId = res.Data,
Createtime = DateTime.Now,
Path = visitPath
};
customerVisitInfoService.AddVisitPic(pic);
}
Logger.Info("visits/add customer: " + model.CustomerId + ", VisitStatus: " + model.VisitStatus + ", ReVisitPlanTime: " + model.ReVisitPlanTime + ", ExpenrienceTime: " + model.ExpenrienceTime);
}
return res;
});
}
//解析base64编码获取图片
private Bitmap Base64ToImg(string base64Code)
{
MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code));
return new Bitmap(stream);
}
public static class StringExtensions
{
///
public class FtpClient
{
///