Exemple #1
0
        public async Task <IActionResult> PostDownloadables([FromForm] Downloadables downloadables)
        {
            try
            {
                if (Request.Form.Files.Count > 0)
                {
                    var    file       = Request.Form.Files[0];
                    var    folderName = Path.Combine("Resources", "Downloads");
                    var    pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                    Random rand       = new Random();
                    var    fileName   = rand.Next(1, 100000).ToString() + ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    var    fullPath   = Path.Combine(pathToSave, fileName);
                    var    dbPath     = Path.Combine(folderName, fileName);

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                    downloadables.SrcURL = dbPath;
                }
                _context.Downloadables.Add(downloadables);
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
            return(CreatedAtAction("GetDownloadables", new { id = downloadables.Id }, downloadables));
        }
Exemple #2
0
        public async Task <IActionResult> PutDownloadables([FromRoute] int id, [FromForm] Downloadables downloadables)
        {
            if (id != downloadables.Id)
            {
                return(BadRequest());
            }

            if (Request.Form.Files.Count > 0)
            {
                try
                {
                    var uploads = Path.Combine(Directory.GetCurrentDirectory(), downloadables.SrcURL);

                    if (System.IO.File.Exists(uploads))
                    {
                        System.IO.File.Delete(uploads);
                    }
                }
                catch (Exception e)
                {
                    //file didnt exist
                }
                var    file       = Request.Form.Files[0];
                var    folderName = Path.Combine("Resources", "Downloads");
                var    pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                Random rand       = new Random();
                var    fileName   = rand.Next(1, 100000).ToString() + ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                var    fullPath   = Path.Combine(pathToSave, fileName);
                var    dbPath     = Path.Combine(folderName, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
                downloadables.SrcURL = dbPath;
            }

            _context.Entry(downloadables).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DownloadablesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }