远程图片服务器

一:问题描述与实现

当服务器有多台的时候,会导致负载均衡后请求的服务器不同,图片不同步的问题,

所以就有图片服务器的诞生,把每台服务器的图片和文件都上传到同一台单独的服务器称为图片服务器。

二:代码实现

1.图片服务器的方法

public class ResourseController : Controller
{
public ActionResult UploadImage( string name,string fileRoot,string fileExtension)
{
byte[] curFile = Convert.FromBase64String(Request["data"]);
if (curFile.Length == 0)
{
var json = new
{
code = 1,
msg = "没有找到要上传的文件呢,请重新选择吧。"
};
return Json(json);
}
if (curFile.Length > 1048576)
{
var json = new
{
code = 1,
msg = "没有找到要上传的文件呢,请重新选择吧。"
};
}
try
{
string FileRoot = fileRoot;

//获取图片文件保存的完整路径
var fileSavePath = Server.MapPath("~/") + "/Images/" + FileRoot + "/Primary/";
//获取新文件名
var uuid = Guid.NewGuid().ToString();
string newFileName = string.Empty;
if (!string.IsNullOrWhiteSpace(name))
{
newFileName = name;
}
else
{
newFileName = uuid + fileExtension;
}
MemoryStream ms = new MemoryStream(curFile);
FileStream fs = new FileStream(fileSavePath + newFileName, FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs = null;
ms = null;
//合并路径
var path = Path.Combine(fileSavePath, newFileName);
//保存原始图
//保存缩略图
var thumbnailPath = Server.MapPath("~/") + "/Images/" + FileRoot + "/";
//合并路径
var thumbpath = Path.Combine(thumbnailPath, newFileName);
ImageHelper.SaveThumbnail(path, thumbpath, 270, 205);
return Json(new
{
code = 0,
msg = "咦?好像上传成功了呢。",
data = new
{
src = "Images" + "/" + FileRoot + "/" + newFileName,
title = "这是图片的标题啊。"
}
});
}
catch (Exception ex)
{
var json = new
{
code = 1,
msg = "出错了,错误信息:" + ex.Message
};
return Json(json);
}
}

public ActionResult UploadFile(string fileExtension)
{
byte[] curFile = Convert.FromBase64String(Request["data"]);
if (curFile.Length == 0)
{
var json = new
{
code = 1,
msg = "没有找到要上传的文件呢,请重新选择吧。"
};
return Json(json);
}
try
{
//获取图片文件保存的完整路径
var fileSavePath = Server.MapPath("~/") + "/Files/";
//获取新文件名
var uuid = Guid.NewGuid().ToString();
var newFileName = uuid + fileExtension;
MemoryStream ms = new MemoryStream(curFile);
FileStream fs = new FileStream(fileSavePath + newFileName, FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs = null;
ms = null;
return Json(new
{
code = 0,
msg = "咦?好像上传成功了呢。",
data = new
{
src = "Files/" + newFileName,
title = newFileName
}
});
}
catch (Exception ex)
{
var json = new
{
code = 1,
msg = "出错了,错误信息:" + ex.Message
};
return Json(json);
}
}
}

在这里有一步操作是压缩图片,为了让前台反应的速度更快,显示的只是缩略图,如何要显示原图,单独做原图显示。

2.多台服务器的上传方法 (核心)

public class UploadController : Controller
{
//目前只支持单文件
[HttpPost]
public ActionResult UploadImage(int type)
{

var files = Request.Files;


if (files.Count == 0)
{
var json = new
{
code = 1,
msg = "没有找到要上传的文件呢,请重新选择吧。"
};
return Json(json);
}
try
{
var curFile = files[0];
var data= SaveHelper.SavePictureToServer(curFile,type,"");
return Json(data);
}
catch (Exception ex)
{
var json = new
{
code = 1,
msg = "出错了,错误信息:" + ex.Message
};
return Json(json);
}
}
[HttpPost]
public ActionResult UploadFile()
{
var files = Request.Files;

if (files.Count == 0)
{
var json = new
{
code = 1,
msg = "没有找到要上传的文件呢,请重新选择吧。"
};
return Json(json);
}
try
{
var curFile = files[0];
var fileName = curFile.FileName;
var type = fileName.Substring(fileName.LastIndexOf('.'));
byte[] b = new byte[curFile.ContentLength];
Stream fs = curFile.InputStream;
fs.Read(b, 0, curFile.ContentLength);
return Json("");
}
catch (Exception ex)
{
var json = new
{
code = 1,
msg = "出错了,错误信息:" + ex.Message
};
return Json(json);
}
}
// GET: Update
public ActionResult Index()
{
return View();
}
}

SavePictureToServer方法

public static RemoteJsonModel SavePictureToServer(HttpPostedFileBase file,int type,string name)
{
var curFile = file;
var fileName = curFile.FileName;
var fileExtension = fileName.Substring(fileName.LastIndexOf('.'));
byte[] b = new byte[curFile.ContentLength];
Stream fs = curFile.InputStream;
fs.Read(b, 0, curFile.ContentLength);
string postData = string.Empty;
if (string.IsNullOrWhiteSpace(name))
{
postData= "fileRoot=" + ImageType.ToDirectory(type) + "&fileExtension=" + fileExtension + "&data=" + HttpUtility.UrlEncode(Convert.ToBase64String(b));
}
else
{
postData = "fileRoot=" + ImageType.ToDirectory(type) + "&name="+name + "&fileExtension=" + fileExtension + "&data=" + HttpUtility.UrlEncode(Convert.ToBase64String(b));
}
var webclient = new WebClient();
webclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
string remoteUrl = GlobalVars.PathRootSave;
byte[] buffer = webclient.UploadData(remoteUrl, "POST", byteArray);
var msg = Encoding.UTF8.GetString(buffer);
var data = JsonHelper.ParseJSON<RemoteJsonModel>(msg);
return data;
}

public class RemoteJsonModel
{
public string code { get; set; }

public string msg { get; set; }

public Data data { get; set; }
}

public class Data
{
public string src { get; set; }
public string title { get; set; }
}

原文地址:https://www.cnblogs.com/smalldragon-hyl/p/8527105.html