Reputation: 23
How to upload multiple images?
I want to upload multiple images into the database; I select the multiple images and then hit the upload button, but it inserts only one image. Can any expert tell me where I am going wrong?
Image
class:
public partial class Image
{
public int imgID { get; set; }
public string ImgPath { get; set; }
public long ProdID { get; set; }
public virtual Product Product { get; set; }
}
Controller:
public ActionResult AddNewProducts(ProductViewModel prod, List file)
{
try
{
List PTlist = _IproductType.PTList();
ViewBag.Ptlist = new SelectList(PTlist, "PType_ID", "P_Name");
// Product Color List
List pColorList = _IProductColor.PColorlist();
ViewBag.pColor_List = new SelectList(pColorList, "C_ID", "C_Name_OR_Code");
List pSizeList = _ISize.pSizeList();
ViewBag.pSizeLists = new SelectList(pSizeList, "S_ID", "S_Size");
string PathDB = string.Empty;
Image img = new Image();
foreach (HttpPostedFileBase files in file)
{
string filename = Path.GetFileName(files.FileName);
string _filename = DateTime.Now.ToString("yymmssff") + filename;
string extension = Path.GetExtension(files.FileName);
string path = Path.Combine(Server.MapPath("~/Upload/"), _filename);
PathDB = "~/Upload/" + _filename;
if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".jpg" || extension.ToLower() == ".png")
{
if (files.ContentLength <= 1000000)
{
img = new Image();
img.imgID = prod.ImgID;
img.ImgPath = PathDB;
img.ProdID = prod.ProductID;
}
else
ViewBag.sizemsg = "Size limit exceeded";
}
else
ViewBag.fileformat = "File is not the correct format";
}
}
}
Upvotes: 1
Views: 639
Reputation: 9804
As you referenced the MCV pattern and used terms like "upload", I am going to asume this is Web Development. I am also asuming your pattern is:
The "reconsider this path" answer
If you do, that is definitely a bad approach. Transfering bulk data to only actually use a fraction of it? A very common beginners mistake with Web and Database Development. Putting a lot of CPU work on the Server side is another one.
Whatever processing you are doing, you should ideally be doing it on the client side: Browser Plugin. Custom Client Side applicaiton. JavaScript element. Even just demanding a specific format from the user. That sort of thing. And then only upload that specific image.
If the server never has to see a bit from those images you do not want, it is a good design.
Staging table/database
This is the asnwer you are really comited to your approach or it is not a option for some reason. In order to have the server process and display a image, it must be saved on the server side.
In memory is not really an option. With a massively paralell design and ThreadPools of 50-100 Threads per CPU core, WebServers are notoriously easy to run out of resources, particular memory. And that asumes you can actually persist something in memory like that to begin with (the design does not make that easy, to get you off that path).
So the next best bet would be a staging table or temp directory. Something where you can:
Personally I would advise for a seperate set of DB Tables. In our day, temp direcotiries are a security vulnerability that no admin should accept without looking for alternatives.
Upvotes: 1