コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,Fiyat,Aciklama,Model,Resim")] ArabaGaleri arabaGaleri, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                if (file != null || file.Length != 0)
                {
                    // Create a File Info
                    FileInfo fi = new FileInfo(file.FileName);

                    // This code creates a unique file name to prevent duplications
                    // stored at the file location
                    var newFilename = arabaGaleri.Id + "_" + String.Format("{0:d}",
                                                                           (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension;
                    var webPath = hostingEnvironment.WebRootPath;
                    var path    = Path.Combine("", webPath + @"\images\" + newFilename);

                    // IMPORTANT: The pathToSave variable will be save on the column in the database
                    var pathToSave = newFilename;

                    // This stream the physical file to the allocate wwwroot/ImageFiles folder
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    // This save the path to the record
                    arabaGaleri.Resim = pathToSave;
                    _context.Update(arabaGaleri);

                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(arabaGaleri));
        }
コード例 #2
0
 public IActionResult About(ArabaGaleri arabaGaleri)
 {
     return(View());
 }