示例#1
0
        public async Task <IActionResult> Post()
        {
            try
            {
                var httpRequest = HttpContext.Request;
                if (httpRequest.Form.Files.Count > 0)
                {
                    foreach (var file in httpRequest.Form.Files)
                    {
                        var ekstenzija = Path.GetExtension(file.FileName);

                        var settings                 = applicationConfigurationService.GetUploadSettings();
                        var dozvoljeneExtenzije      = settings.DozvoljeneEkstenzije;
                        var dozvoljeneExtenzijeLista = new List <string>(dozvoljeneExtenzije);

                        if (!dozvoljeneExtenzijeLista.Contains(ekstenzija))
                        {
                            return(BadRequest("Nije dozvoljena ekstenzija"));
                        }

                        var maksimalnaVelicinaDokumenta = settings.MaksimalnaVelicinaMB;
                        var fileSizeMB = (file.Length / 1024f) / 1024f;

                        if (maksimalnaVelicinaDokumenta < fileSizeMB)
                        {
                            return(BadRequest("Prevelik file"));
                        }

                        var result = await uploadService.Post(file);

                        var uploadResult = new UploadResultModel
                        {
                            Options = httpRequest.Form,
                            Result  = result
                        };

                        return(Ok(uploadResult));
                    }
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (System.Exception e)
            {
                return(BadRequest());
            }
        }
示例#2
0
        async Task <ServiceResult <FileResult> > IUploadService.Get(int id)
        {
            var dokument = context.Dokumenti.FirstOrDefault(a => a.Id == id);

            if (dokument == null)
            {
                return(Error("Dokument ne postoji"));
            }

            var folderPath    = applicationConfigurationService.GetUploadSettings().PutanjaFoldera;
            var localFilePath = Path.Combine(folderPath, dokument.Putanja);

            var fileName = dokument.Naziv;

            if (!File.Exists(localFilePath))
            {
                return(Error("Dokument ne postoji"));
            }
            else
            {
                using (var document = File.Open(localFilePath, FileMode.Open))
                {
                    //Response.Headers.Add("Content-Disposition", new Microsoft.Extensions.Primitives.StringValues("attachment"));
                    var ekstenzija = Path.GetExtension(localFilePath);

                    byte[] bytes = new byte[document.Length];
                    await document.ReadAsync(bytes, 0, bytes.Length);

                    return(Ok(new FileResult
                    {
                        Bytes = bytes,
                        MimeType = MimeTypeMap.GetMimeType(ekstenzija),
                        Naziv = dokument.Naziv
                    }));
                }
            }
        }
示例#3
0
        //async Task<ServiceResult<FileResponse>> Download(int id)
        async Task <FileResponse> IPrilogZahtjevaService.Download(int id)
        {
            FileResponse fileResponse = null;

            var prilogZahtjeva = context.Dokumenti.Where(d => d.Id == id).FirstOrDefault();

            /*fileResponse = new FileResponse()
             * {
             *  Naziv = prilogZahtjeva.Naziv,
             *  Bytes = package.GetAsByteArray(),
             *  ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
             * };
             *
             * return Ok(fileResponse);*/

            /* if (prilogZahtjeva == null)
             *   return Error("filename not present");*/
            var folderPath = applicationConfigurationService.GetUploadSettings().PutanjaFoldera;

            var path = Path.Combine(folderPath, prilogZahtjeva.Putanja);

            var memory = new MemoryStream();

            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;

            fileResponse = new FileResponse()
            {
                Naziv = prilogZahtjeva.Naziv,
                //Bytes = package.GetAsByteArray(),
                //ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            };

            return(fileResponse);

            // return File(memory, GetContentType(path), Path.GetFileName(path));
        }