【问题标题】:C# - Use WebImage.GetImageFromRequest() with multiple uploadsC# - 使用 WebImage.GetImageFromRequest() 进行多次上传
【发布时间】:2018-04-16 17:46:25
【问题描述】:

我正在尝试从一个表单上传多张图片。然后使用 WebImage.GetImageFromRequest() 抓取图像。这适用于单个上传字段,但我不确定如何使用它来获取多个文件。

我的 HTML 表单:

<input id="uploadBtn1" type="file" name="Image1" accept=".jpg,.png" class="upload"/>
<input id="uploadBtn2" type="file" name="Image2" accept=".jpg,.png" class="upload"/>
<input id="uploadBtn3" type="file" name="Image3" accept=".jpg,.png" class="upload"/>

我的 C# 代码

if (IsPost)
{
 photo1 = WebImage.GetImageFromRequest("image1");
 newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo1.FileName);
 imagePath = @"branding\" + newFileName;
 photo1.Save(@"~\" + imagePath);

 photo2 = WebImage.GetImageFromRequest("image2");
 newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo2.FileName);
 imagePath = @"branding\" + newFileName;
 photo2.Save(@"~\" + imagePath);

 photo3 = WebImage.GetImageFromRequest("image3");
 newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo3.FileName);
 imagePath = @"branding\" + newFileName;
 photo3.Save(@"~\" + imagePath);
}

这会引发错误: System.NullReferenceException:对象引用未设置为对象的实例。 第 101 行:newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo3.FileName);

我认为我使用了不正确的语法:photo1 = WebImage.GetImageFromRequest("image1");

如何指定它来获取“image1”上传字段?还有“image2”、“image3”等?

【问题讨论】:

    标签: c# razor file-upload webmatrix


    【解决方案1】:

    你包括了吗

    enctype="multipart/form-data"
    

    进入你的表格?

    例子:

    <form action="" method="post" enctype="multipart/form-data">  
    

    这是一个如何遍历多个文件的示例:

    https://forums.asp.net/t/2024891.aspx?WebImage+helper+multiple+image+upload
    

    【讨论】:

    • 那个链接就是答案。只需要指定字段名:photo1 = WebImage.GetImageFromRequest("Image1");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2018-10-27
    • 2018-06-19
    • 2016-06-16
    • 2015-09-10
    相关资源
    最近更新 更多