Я думаю, у вас есть два варианта;
теперь, если веб-сайт сервер под названием сервера 1, сервер базы данных называется сервер 2.
Option1, вы можете загрузить изображение в папку на сервере 1, база данных просто хранить имя изображения, путь к изображению ,
//get the file name of the posted image
string imgName = image.FileName.ToString();
String path = Server.MapPath("~/ImageStorage");//Path
//Check if directory exist
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
}
//sets the image path
string imgPath = Path.Combine(path, imgName);
//get the size in bytes that
int imgSize = image.PostedFile.ContentLength;
if (image.PostedFile != null)
{
if (image.PostedFile.ContentLength > 0)//Check if image is greater than 5MB
{
//Save image to the Folder
image.SaveAs(imgPath);
//also save image path to database
//......
}
}
Во-вторых, вы можете напрямую сохранить изображение в базу данных на Сервера2, тип столбца может использовать изображение, байт, или двоичный BLOB-
public static void PerisitImage(string path, IDbConnection connection)
{
using (var command = connection.CreateCommand())
{
Image img = Image.FromFile (path);
MemoryStream tmpStream = new MemoryStream();
img.Save (tmpStream, ImageFormat.Png); // change to other format
tmpStream.Seek (0, SeekOrigin.Begin);
byte[] imgBytes = new byte[MAX_IMG_SIZE];
tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "payload";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery();
}
}
Вы можете загружать файлы в папку на сервере 1 и создать виртуальный каталог на сервере 2 и путь для этого каталога будет иметь папку Server1. – Mairaj
Спасибо Mairaj, но мне нужны изображения также на соответствующих серверах, то есть на сервере2. – Deeps
Да, он будет доступен на обоих серверах. – Mairaj