473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Uploading images in MVC 5

PsychoCoder
465 Expert Mod 256MB
I'm currently working on an image gallery (or products with their images) and am getting a weird error in the UploadImageMethod in my ProductsController. Here's the method, then I'll explain where the error is:

Expand|Select|Wrap|Line Numbers
  1.  [HttpPost]
  2.     public ActionResult UploadImageMethod()
  3.     {
  4.         //make sure we have files to upload
  5.         if (Request.Files.Count != 0)
  6.         {
  7.             //Parallel.For loop to loop through each image being uploaded
  8.             Parallel.For(0, Request.Files.Count, index =>
  9.                 {
  10.                     //new HttpPostedFileBase to hold each image with
  11.                     HttpPostedFileBase file = Request.Files[index];
  12.  
  13.                     //get the file size
  14.                     int size = file.ContentLength;
  15.  
  16.                     //get the file name
  17.                     string name = file.FileName;
  18.  
  19.                     //save the image to our desired directory
  20.                     file.SaveAs(Server.MapPath("~/Content/ProductImages/") + name);
  21.  
  22.                     //now create a new Product and set it's properties
  23.                     Product p = new Product()
  24.                     {
  25.                         ProductId = Guid.NewGuid(),
  26.                         ProductName = name,
  27.                         ProductImages.Add(new ProductImage() { Path = Server.MapPath("~/Content/ProductImages/") + name, AltText = name })
  28.                     };
  29.  
  30.                     //add it to the database
  31.                     db.Products.Add(p);
  32.  
  33.                     //save the changes
  34.                     db.SaveChanges();
  35.                 });
  36.             return Content("Success");
  37.         }
  38.         return Content("failed");
  39.     }
  40.     }
I'm getting the error on this line:

Expand|Select|Wrap|Line Numbers
  1.  ProductImages.Add(new ProductImage() { Path = Server.MapPath("~/Content/ProductImages/") + name, AltText = name })
It says "invalid initializer member declarator" , it also says ProductImages does not exist in the current context. If you need to see the Product class here it is:

Expand|Select|Wrap|Line Numbers
  1. public class Product
  2.     {
  3.         public Product()
  4.         {
  5.             ProductImages = new List<ProductImage>();
  6.         }
  7.  
  8.         public int ProductId { get; set; }
  9.  
  10.         public string ProductName { get; set; }
  11.         public double ProductPrice { get; set; }
  12.         public int ProductQuantity { get; set; }
  13.  
  14.         public virtual List<ProductImage> ProductImages { get; set; }
  15.     }
Can someone tell me what I'm doing wrong?
Jul 18 '15 #1
0 1465

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: laredotornado | last post by:
Hi, I'm using PHP 4 and I am submitting some images in a form with <form name=addProductForm enctype="multipart/form-data" method=post action="add_product_response.php"> <input type=file...
2
by: JonChart | last post by:
Hi all, I would like to know ho to validate a file name when uploading via asp. I need to upload 2 images and the file names must be the same... 1) browse for 1st image. 2) image path and...
3
by: Gavin | last post by:
I need some help, I need the code to allow people that visit my website to be able to upload pictures to a file on my web server. I have been able to get close, but not quite there yet. Set fso...
1
by: joe | last post by:
Any articles relating with Uploading images files to server and resize the image by asp.net 2.0
1
by: Nerry | last post by:
Hello everyone, I have ASP webpages, but I have a problem uploading my images via ftp, keep getting an error: An error occurred copying a file to the ftp server. Make sure you have permissions...
0
by: LizB | last post by:
Hi guys, I would appreciate very much any help that anybody can give me with the following problem I have. I am trying to upload images using PHP move_uploaded_file, but for some reason the...
4
by: tddeffner | last post by:
Hey, I was wondering if you guys could help me out. I need to find a way to automatically generate thumbnails as seperate images when uploading. I keep hearing that JavaScript can do it. Anyone...
2
by: zhiyuan | last post by:
Hello, so here is my code for this add score, images php. but when i click the submit bottom the images won't go to the pointed folder thats under my website root folder. 1st question: could this...
1
by: jagadishk | last post by:
hi this is my code for uploading images into data base which is woring fine, but i want to upload multiple images an any one help me. <?php include 'config.php'; error_reporting(E_ALL ^...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.