Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用asp.net MVC拖放上载到aws s3_C#_Asp.net Mvc_Amazon Web Services_Amazon S3 - Fatal编程技术网

C# 使用asp.net MVC拖放上载到aws s3

C# 使用asp.net MVC拖放上载到aws s3,c#,asp.net-mvc,amazon-web-services,amazon-s3,C#,Asp.net Mvc,Amazon Web Services,Amazon S3,我在一个基本的mvc项目上上传文件时遇到了一些问题。。。我有一个拖放功能,我想连接到aws s3。我可以使用当前应用程序将文件放入本地文件夹。。并按照这里的步骤->——上传一个单独的特定文件,并获得成功。但是,我无法将这两者结合起来,以便我拖放的文件最终会出现在我的存储桶中 以下是我迄今为止的所有代码: 索引 @{ ViewBag.Title=“Index”; } 拖放文件上载 把你的文件放在这里 上载的文件: #下降区{ 背景图像:url(“http://xx.png"); 背景重复:

我在一个基本的mvc项目上上传文件时遇到了一些问题。。。我有一个拖放功能,我想连接到aws s3。我可以使用当前应用程序将文件放入本地文件夹。。并按照这里的步骤->——上传一个单独的特定文件,并获得成功。但是,我无法将这两者结合起来,以便我拖放的文件最终会出现在我的存储桶中

以下是我迄今为止的所有代码:

索引

@{
ViewBag.Title=“Index”;
}
拖放文件上载
把你的文件放在这里
上载的文件:
#下降区{ 背景图像:url(“http://xx.png"); 背景重复:无重复; 背景位置:中心; 背景色:#fff; 边框:黑色虚线1px; 高度:500px; 文本对齐:居中; 颜色:黑色; } .主动下降{ 背景:#739cfb!重要; 边框:纯色2件套蓝色!重要; 不透明度:.5; 颜色:黑色!重要; } @节脚本{ $(函数(){ $(“#dropArea”).filedrop({ url:'@url.Action(“上载文件”), 允许的文件类型:['image/jpeg','image/png','image/gif'], 允许的文件扩展名:['.jpg'、'.jpeg'、'.png'、'.gif'], paramname:'文件', 最大文件数:5, maxfilesize:5,//以MB为单位 dragOver:函数(){ $('#dropArea').addClass('active-drop'); }, dragLeave:function(){ $(“#dropArea”).removeClass('active-drop'); }, drop:函数(){ $(“#dropArea”).removeClass('active-drop'); }, 毕竟:功能(e){ $('#dropArea').html('文件上载成功'); }, 上传完成:函数(i、文件、响应、时间){ $(“#uploadList”).append(“
  • ”+file.name+”
  • ”) } }) })
    }
    我将按照您发布问题的顺序开始

    Index.cshtml

    1) 请验证您在
    URL:'@URL.Action(“UploadFiles”)
    中引用的URL是否正确。我认为应该是
    url:'@url.Action(“上传文件”,“主页”)

    2) 请验证是否已通过脚本标记添加jquery

    HomeController.cs

    1) 索引函数应该只返回一个视图

    2) 在构造函数中实例化一个新的AmazonS3Uploader

    3) 将所有文件路径保存在一个列表中,循环遍历它们,并将每个路径传递给amazonS3.UploadFile(path)`以便上传到S3

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MVCDragDrop.Controllers
    {
        public class HomeController : Controller
        {
            private AmazonS3Uploader amazonS3;
    
            public HomeController()
            {
                amazonS3 = new AmazonS3Uploader();
            }
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
            {
                List<string> paths = new List<string>();
    
                foreach (var file in files) {
                    string filePath = Guid.NewGuid() + Path.GetExtension(file.FileName);
                    string destPath = Path.Combine(Server.MapPath("~/UploadedFiles"), filePath);
                    paths.Add(destPath)
                    file.SaveAs(destPath);
                }
    
                foreach(var path in paths){
                    amazonS3.UploadFile(path);
                }
    
                return Json("file uploaded successfully");
            }
        }
    }
    

    希望我能提供帮助:)

    您不应该将保存文件的路径传递到
    amazonS3.UploadFile()吗?当您将所有文件保存到本地文件夹时,也将其路径保存在一个数组中,该数组应逐个传递到
    amazonS3.UploadFile()非常好,谢谢!。。还有一个问题。。如何绕过“UploadedFiles”文件夹直接转到s3?现在我已经启动并运行了,我希望能够上传大文件而不需要额外的本地备份。我的目标是从各种外部硬盘上传到s3。非常感谢!我认为不可能直接上传,因为
    PutRequestObject
    需要上传文件的路径,因此上传前需要下载/保存文件。如果你觉得答案很有用,那就投票并接受它吧:)。谢谢。还有一件事,文件上传后,您可以使用
    file.delete(path)
    删除本地临时文件夹中的文件,以便在前进过程中删除跟踪:p
    using System;
    using Amazon.S3;
    using Amazon.S3.Model;
    
    namespace MVCDragDrop
    {
        public class AmazonS3Uploader
        {
            private string bucketName = "xxxxx";
            private string keyName = "";
            private AmazonS3Client client;
    
            public AmazonS3Uploader()
            {
                client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
            }
    
            public void UploadFile(string filePath)
            {
    
                try {
                    PutObjectRequest putRequest = new PutObjectRequest {
                        BucketName = bucketName,
                        Key = keyName,
                        FilePath = filePath,
                        ContentType = "plain/text"
                    };
    
                    PutObjectResponse response = client.PutObject(putRequest);
                }
                catch (AmazonS3Exception amazonS3Exception) {
                    if (amazonS3Exception.ErrorCode != null &&
                        (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                        ||
                        amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) {
                        throw new Exception("Check the provided AWS Credentials.");
                    } else {
                        //throw new Exception("Error occurred: " + amazonS3Exception.Message);
                    }
                }
            }
        }
    }