示例#1
0
        /// <summary>
        /// Saves an uploaded file to disk.
        /// </summary>
        private async Task <(string LocalPath, string UrlPath)> SaveUploadAsync(IFormFile file, string key, IMediaHandler handler)
        {
            var ext      = Path.GetExtension(file.FileName);
            var fileName = key + ext;
            var filePath = Path.Combine(_env.WebRootPath, "media", fileName);

            using (var localStream = new FileStream(filePath, FileMode.CreateNew))
                using (var sourceStream = file.OpenReadStream())
                    await sourceStream.CopyToAsync(localStream);

            using (var frame = await handler.ExtractThumbnailAsync(filePath, file.ContentType))
                await MediaHandlerHelper.CreateThumbnailsAsync(filePath, frame);

            return(filePath, $"~/media/{fileName}");
        }
示例#2
0
    public static void PrepareMediaHandler(Object value, string SessionHandlerObject, bool SaveAsFile, bool EncryptedFile)
    {
        string FileName = "";

        byte[] FullContent = null;

        HttpContext.Current.Session.Remove(SessionHandlerObject);
        HttpContext.Current.Session.Remove(SessionHandlerObject + "FileName");

        try
        {
            if (SaveAsFile)
            {
                if (value == null)
                {
                    return;
                }

                string FilePath = "";
                try
                {
                    FilePath = HttpContext.Current.Server.MapPath(value.ToString());
                }
                catch
                {
                    if (value.ToString().StartsWith("/"))
                    {
                        FilePath = HttpContext.Current.Server.MapPath("~" + value.ToString());
                    }
                }

                //busca arquivo
                if (!File.Exists(FilePath))
                {
                    return;
                }

                using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    FullContent = new byte[fs.Length];
                    fs.Read(FullContent, 0, (int)fs.Length);

                    if (EncryptedFile)
                    {
                        FullContent = Crypt.Decript_AES(FullContent);
                    }

                    FileName = Path.GetFileName(value.ToString());

                    HttpContext.Current.Session[SessionHandlerObject] = Convert.ToBase64String(FullContent);
                    HttpContext.Current.Session[SessionHandlerObject + "FileName"] = value.ToString();                      // FileName;
                }
            }
            else
            {
                int ContentStart  = 0;
                int ContentLenght = 0;

                FullContent = (byte[])value;
                if (FullContent == null)
                {
                    return;
                }

                if (EncryptedFile)
                {
                    FullContent = Crypt.Decript_AES(FullContent);
                }

                MediaHandlerHelper.ParseFileContent(FullContent, out ContentStart, out ContentLenght, out FileName);
                HttpContext.Current.Session[SessionHandlerObject] = Convert.ToBase64String(FullContent);
                HttpContext.Current.Session[SessionHandlerObject + "FileName"] = FileName;
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            value       = null;
            FullContent = null;
        }
    }