繁体   English   中英

使用ASP.Net MVC 3将图像上传到Amazon S3

[英]Uploading an Image to Amazon S3 using ASP.Net MVC 3

我在控制器端遇到问题,使用ASP.Net MVC 3试图将图像上传到亚马逊s3。 这是我所拥有的和错误。

这是我的HTML表单。

@using (Html.BeginForm())
{
    <div class="in forms">

        <input type="file" id="file" name="file" class="box" /></p>

        <p><input type="submit" value="Upload" id="btnSubmit" class="com_btn" /></p>

}

这是我的控制器中的代码

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("*redacted*","*redacted*");
        if (file.ContentLength > 0)
        {
            var request = new PutObjectRequest();
            request.WithBucketName("*redacted*");
            request.WithKey(file.FileName);
            request.FilePath = Path.GetFullPath(file.FileName);
            request.ContentType = file.ContentType;
            request.StorageClass = S3StorageClass.ReducedRedundancy;
            request.CannedACL = S3CannedACL.PublicRead;
            client.PutObject(request);
            return Redirect("UploadSuccess");
        }
        return RedirectToAction("Index");
    }

我得到的错误是。

'/'应用程序中的服务器错误。

你调用的对象是空的。

描述:执行当前Web请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:

第28行:if(file.ContentLength> 0)

这有帮助吗?

using (@Html.BeginForm(new { enctype = "multipart/form-data" }))

你被劫持了 - 上传文件

使用断点并调试代码。 查看对象是否为null。 访问null对象的属性/方法通常会给您带来此错误。

@using (Html.BeginForm("Index", "YourController", FormMethod.Post, new { enctype = "multipart/form-data" })) 

并改变

if (file.ContentLength > 0)

if (file == null || file.ContentLength <= 0)
{
    // Add some client side error message.

    // Return the view
    return View();
}

// Upload...
var request = new PutObjectRequest();
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM