Exemplo n.º 1
0
        /*
         * protected async Task<string> SaveFileAsync(string contentUrl, string fileNamePrefix)
         * {
         *  try
         *  {
         *
         *      // save file first
         *      if (HttpContext.Request.Form.Files.Any())
         *      {
         *          // remove old files
         *          var fileNames = contentUrl?.Split('|', StringSplitOptions.RemoveEmptyEntries);
         *          foreach (var item in fileNames)
         *          {
         *              if (System.IO.File.Exists(item))
         *              {
         *                  System.IO.File.Delete(item);
         *              }
         *          }
         *
         *          for (int i = 0; i < HttpContext.Request.Form.Files.Count; i++)
         *          {
         *              var file = HttpContext.Request.Form.Files[i];
         *
         *              var ext = Path.GetExtension(file.FileName);
         *              var fileName = $"{fileNamePrefix ??= "_"}{Guid.NewGuid()}{ext}";
         *
         *              var path = Path.Combine(_env.ContentRootPath, "files", fileName);
         *              using (var stream = new FileStream(path, FileMode.Create))
         *              {
         *                  await file.CopyToAsync(stream);
         *              }
         *
         *              contentUrl += path + "|";
         *          }
         *      }
         *
         *      // remove last |
         *      var fileUrl = contentUrl?.Substring(0, contentUrl.Length - 1);
         *
         *      return fileUrl;
         *  }
         *  catch (Exception ex)
         *  {
         *
         *      throw new Exception("File not upload", ex);
         *  }
         *
         * }
         */

        protected async Task <(string path, string virtualPath)> SaveFileByteAsync(string contentUrl, string fileNamePrefix, string folder = "files", bool saveToDisk = false)
        {
            try
            {
                var virtualPath = contentUrl;
                var path        = string.Empty;

                // save file first
                if (DataUrl.IsValidContentUrl(contentUrl))
                {
                    var bytes = DataUrl.ToBytes(contentUrl);

                    if (saveToDisk || (bytes.Length / 1024 / 1024) > MaxSizeAllow())
                    {
                        var ext = DataUrl.ToFileExtension(contentUrl);

                        var fileName = $"{fileNamePrefix}{Guid.NewGuid()}.{ext}";

                        virtualPath = $"{folder}/{ fileName }";

                        path = Path.Combine(_env.ContentRootPath, virtualPath);

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await stream.WriteAsync(bytes, 0, bytes.Length);
                        }

                        virtualPath = "/" + virtualPath;
                    }
                }
                return(path, virtualPath);
            }
            catch (Exception ex)
            {
                throw new Exception("File not upload", ex);
            }
        }