Exemple #1
0
        public static async Task <FileResource> MakeFileResourceFromHttpRequest(HttpRequest httpRequest, RioDbContext rioDbContext, HttpContext httpContext)
        {
            var bytes = await httpRequest.GetData();

            var userDto         = UserContext.GetUserFromHttpContext(rioDbContext, httpContext);
            var queryCollection = httpRequest.Query;

            var fileResourceMimeType = FileResourceMimeType.GetFileResourceMimeTypeByContentTypeName(rioDbContext,
                                                                                                     queryCollection["mimeType"].ToString());

            var clientFilename   = queryCollection["clientFilename"].ToString();
            var extension        = clientFilename.Split('.').Last();
            var fileResourceGuid = Guid.NewGuid();

            return(new FileResource
            {
                CreateDate = DateTime.Now,
                CreateUserID = userDto.UserID,
                FileResourceData = bytes,
                FileResourceGUID = fileResourceGuid,
                FileResourceMimeTypeID = fileResourceMimeType.FileResourceMimeTypeID,
                OriginalBaseFilename = clientFilename,
                OriginalFileExtension = extension,
            });
        }
Exemple #2
0
        /// <summary>
        /// Writes the memory stream to the browser.
        /// </summary>
        /// <param name="memoryStream">Memory stream.</param>
        /// <param name="fileName">File name.</param>
        /// <param name="fileResourceMimeType"></param>
        private static void WriteStream(MemoryStream memoryStream, string fileName, FileResourceMimeType fileResourceMimeType)
        {
            var context = HttpContext.Current;

            context.Response.Clear();

            context.Response.AddHeader("Content-Type", fileResourceMimeType.FileResourceMimeTypeName);

            context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", fileName));
            memoryStream.WriteTo(context.Response.OutputStream);
            memoryStream.Close();
            context.Response.End();
        }
Exemple #3
0
        /// <summary>
        /// Writes the memory stream to the browser.
        /// </summary>
        /// <param name="memoryStream">Memory stream.</param>
        /// <param name="fileName"></param>
        /// <param name="fileResourceMimeType"></param>
        private static void WriteStream(MemoryStream memoryStream, string fileName, FileResourceMimeType fileResourceMimeType)
        {
            var context = HttpContext.Current;

            context.Response.Clear();

            context.Response.AddHeader("Content-Type", fileResourceMimeType.FileResourceMimeTypeContentTypeName);

            context.Response.AddHeader("Content-Disposition", $"inline;filename=\"{fileName}\"");
            context.Response.AddHeader("Content-Length", memoryStream.Length.ToString());
            memoryStream.WriteTo(context.Response.OutputStream);
            memoryStream.Close();
            context.Response.End();
        }
        public async Task <ActionResult <object> > CkEditorUpload()
        {
            byte[] bytes;


            using (var ms = new MemoryStream(2048))
            {
                await Request.Body.CopyToAsync(ms);

                bytes = ms.ToArray();
            }

            var userDto         = UserContext.GetUserFromHttpContext(_dbContext, HttpContext);
            var queryCollection = Request.Query;

            var fileResourceMimeType = FileResourceMimeType.GetFileResourceMimeTypeByContentTypeName(_dbContext,
                                                                                                     queryCollection["mimeType"].ToString());

            var clientFilename   = queryCollection["clientFilename"].ToString();
            var extension        = clientFilename.Split('.').Last();
            var fileResourceGuid = Guid.NewGuid();
            var fileResource     = new FileResource
            {
                CreateDate             = DateTime.Now,
                CreateUserID           = userDto.UserID,
                FileResourceData       = bytes,
                FileResourceGUID       = fileResourceGuid,
                FileResourceMimeTypeID = fileResourceMimeType.FileResourceMimeTypeID,
                OriginalBaseFilename   = clientFilename,
                OriginalFileExtension  = extension,
            };

            _dbContext.FileResource.Add(fileResource);
            _dbContext.SaveChanges();

            return(Ok(new { imageUrl = $"/FileResource/{fileResourceGuid}" }));
        }
Exemple #5
0
 protected void ConstructorImpl(string fileName, MemoryStream memoryStream, FileResourceMimeType fileResourceMimeType)
 {
     FileResourceMimeType = fileResourceMimeType;
     MemoryStream         = memoryStream;
     Filename             = fileName;
 }
Exemple #6
0
        public FileResourceResult(string fileName, byte[] fileBytes, FileResourceMimeType fileResourceMimeType)
        {
            var memoryStream = new MemoryStream(fileBytes);

            ConstructorImpl(fileName, memoryStream, fileResourceMimeType);
        }
Exemple #7
0
 /// <summary>
 /// Action Result: File Result constructor for creating a downloadable file result
 /// </summary>
 /// <param name="fileName">Filename here</param>
 /// <param name="memoryStream"></param>
 /// <param name="fileResourceMimeType"></param>
 public FileResourceResult(string fileName, MemoryStream memoryStream, FileResourceMimeType fileResourceMimeType)
 {
     ConstructorImpl(fileName, memoryStream, fileResourceMimeType);
 }