管理面板的文件管理部分

File Management Section for an Admin Panel

我正在为我的客户开发 CRM Web 应用程序。包括客户管理、支付等。

我使用的技术:

现在我的客户想要将像 Dropbox 这样的云文件存储添加到 Web 应用程序。它应该允许上传/删除文件和文件夹(对于管理员)并且应该允许普通用户下载它们。

我认为有一些很好的完整解决方案,所以我不会重新发明轮子。首选开源和免费的。我正在寻找可以设置并添加到现有应用程序的模块。


更新

我认为 SO 社区误解了我的意思。我不是在寻找一个非常繁重的文件管理解决方案。所需要的是像 Masoud Bimar 建议的那样的东西。但我正在寻找更小、更简单的东西。

我只是不想重新发明轮子,从头开始编写代码对我来说有点无聊。

我敢肯定有人已经开发了这个功能。

同样,解决方案应该允许:

我的客户偶尔会用到它,上传的文件不会超过 20 个。也许删除它们并不时上传新的。就是这样。

我用过这个,用 elFinder 库构建。

文件管理器,有 ELFinder.Net。 支持目录、PDF 文件、权限,不错 UI。

非常满意

包信息:

  <package id="bootstrap" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />

github repository

真正重新发明轮子是棘手的,但是使用第 3 方包总是有局限性。

我在 Amazon Web Services 中使用 Bucket S3,并且由于对文件的访问是通过凭据进行的,因此您可以通过在上传具有访问权限的用户时写入数据库来轻松限制对文件的访问。

这里是一个上传和下载代码的例子。

您需要安装 AWS SDK Nuget 包。

https://aws.amazon.com/sdk-for-net/

Link 以及创建凭据的说明

https://docs.aws.amazon.com/en/sdk-for-net/v2/developer-guide/net-dg-setup.html#net-dg-signup

我希望这能在某些方面有所帮助

using Amazon.S3;
using Amazon.S3.Model;

public async Task<IActionResult> Upload(IFormFile file)
{
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKey");
    IAmazonS3 clientAws = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.EUCentral1);
    string urlTemp = Path.GetTempFileName();
    string extension = Path.GetExtension(file.FileName);
    Guid guid = Guid.NewGuid();
    string nameFile = guid + extension;
    string contentType = file.ContentType;

    using (var fileStream = new FileStream(urlTemp, FileMode.Create))
    {
        await file.CopyToAsync(fileStream);
    }

    try
    {
        // simple object put
        using (clientAws)
        {
            var request = new PutObjectRequest()
            {
                BucketName = "yourbucket",
                Key = nameFile,
                FilePath = urlTemp,
                ContentType = contentType
            };
            var response = await clientAws.PutObjectAsync(request);

            //write in your db
        }
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        if (amazonS3Exception.ErrorCode != null &&
            (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
            amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
        {
            Console.WriteLine("Please check the provided AWS Credentials.");
            Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
        }
        else
        {
            Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
        }
    }

    return Ok();
}

public async Task<IActionResult> Download(string file)
{
    try
    {
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials("accessKey", "secretKey");
        IAmazonS3 clientAws = new AmazonS3Client(awsCredentials, Amazon.RegionEndpoint.EUCentral1);

        GetObjectResponse response = new GetObjectResponse();

        string urlTemp = Path.GetTempPath();
        Guid guid = Guid.NewGuid();
        string nameFile = guid + ".pdf";

        try
        {
            // simple object put
            using (clientAws)
            {
                GetObjectRequest request = new GetObjectRequest();

                request.BucketName = "yourBucket";
                request.Key = file;
                response = await clientAws.GetObjectAsync(request);
                CancellationTokenSource source = new CancellationTokenSource();
                CancellationToken token = source.Token;
                await response.WriteResponseStreamToFileAsync(urlTemp + nameFile, true, token);


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

                var fsResult = new FileStreamResult(memory, "application/pdf");
                return fsResult;

            }

        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            if (amazonS3Exception.ErrorCode != null &&
                (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
            {
                Console.WriteLine("Please check the provided AWS Credentials.");
                Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
            }
            else
            {
                Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message);
            }

        }
    }
    catch (Exception ex)
    {
        //throw;
    }

    return View();
}

这是一个简单的文件管理系统示例,使用标准 ASP.NET MVC 控制器和操作,entity framework 和很棒的 javascript 库 dropzone 来上传多个文件使用 AJAX.

每个客户都会有一个文件列表,因此我们需要将此信息存储在数据库中 table,这是模型:

public class FileUpload
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string Filename { get; set; }
    public string OriginalFilename { get; set; }
    public string ContentType { get; set; }
}

然后,控制器将处理文件上传和下载等...

public class FileUploadController : Controller
{
    private SomeDbContext db = new SomeDbContext();

    // You should store the following settings in your Web.config or in your DB, I just put them here for demo purposes

    // This is the root folder where your files will be saved
    private string FilesRoot = @"c:\temp";

    // Accepted file types and maximum size, for security (it should match the settings in your view)
    private string[] AcceptedFiles = new string[] { ".jpg", ".png", ".doc" };
    private int MaxFileSizeMB = 10;

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult UploadFiles(int customerId)
    {
        foreach(string file in Request.Files)
        {
            var upload = Request.Files[file];

            if (!ValidateUpload(upload))
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);

            string filename = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);

            // Save the file in FilesRoot/{CustomerId}/{GUID}
            string savePath = Path.Combine(FilesRoot, customerId.ToString());
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            upload.SaveAs(Path.Combine(savePath, filename));

            // Save file info to database
            var fileUpload = new FileUploadModel()
            {
                CustomerId = customerId,
                Filename = filename,
                OriginalFilename = upload.FileName,
                ContentType = upload.ContentType
            };

            db.FileUploads.Add(fileUpload);
            db.SaveChanges();
        }

        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

    private bool ValidateUpload(HttpPostedFileBase upload)
    {
        if (!AcceptedFiles.Contains(Path.GetExtension(upload.FileName)))
            return false;
        else if (upload.ContentLength > MaxFileSizeMB * 1048576)
            return false;

        return true;
    }

    public ActionResult DownloadFile(int id)
    {
        var fileUpload = db.FileUploads.FirstOrDefault(x => x.Id == id);
        if (fileUpload == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.NotFound);
        }

        string path = Path.Combine(FilesRoot, fileUpload.CustomerId.ToString(), fileUpload.Filename);

        byte[] fileContents = System.IO.File.ReadAllBytes(path);

        return File(fileContents, fileUpload.ContentType, fileUpload.OriginalFilename);
    }

    public ActionResult ListFiles(int customerId)
    {
        var files = db.FileUploads.Where(x => x.CustomerId == customerId);
        return View(files.ToList());
    }


    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

最后,这是上传文件的视图,您可能需要添加另一个视图来显示给定客户的文件列表,但这应该很简单。

@{
    ViewBag.Title = "Upload Files";
}

<div>
    <button id="selectFile">Click to Browse File</button>
</div>

@using (Html.BeginForm("UploadFiles", "FileUpload", FormMethod.Post, new { id = "uploadForm", @class = "dropzone dropzone-area" }))
{
    @Html.AntiForgeryToken()

    @Html.Hidden("CustomerId", 1)

    <div class="dz-message">Drop File Here To Upload</div>

    <div class="fallback">
        <input name="file" type="file" multiple />
    </div>
}


@section Scripts {
    <script src="@Url.Content("~/Scripts/dropzone.js")"></script>

    <script type="text/javascript">
        Dropzone.options.selectForm = {
            paramName: 'file',
            maxFilesize: 10,
            maxFiles: 10,
            acceptedFiles: '.jpg,.png,.doc,.pdf',
            addRemoveLinks: false
        };

        $('#selectFile').on('click', function () {
            $('#uploadForm').trigger('click');
        });
    </script>
}