在 ASP .NET MVC 中将图像上传到 jpg

本文关键字:jpg 图像 ASP NET MVC | 更新日期: 2023-09-27 18:36:15

我仍然是Asp .Net MVC和C#的新手。我正在学习如何做某些事情,并且在获取按钮以加载我的视图时遇到了问题。我了解到我不需要像通常在 .NET MVC 中那样的事件侦听器,所以我使用 @Ajax.ActionLink 作为我的按钮。但是,当我单击我的按钮时,它不会加载我的视图。我不知道为什么?我无法使按钮加载我的视图。

这是我所做的。在我的共享文件夹中,我有一个包含按钮的布局页面。我插入了这个按钮:

<li>@Ajax.ActionLink("Res-TestImageExtension","TestImageExtension","TestImageExtension","",App.ViewOptions.appAjaxOptions,App.ViewOptions.blueButtonHtmlAttributes)</li>

在我的控制器文件夹中,我插入了一个控制器,其中包含以下内容:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HexaPod.Controllers
{
public class TestImageExtensionController : Controller
{
// For multiple file select
    [HttpPost]
    public ActionResult Create(List<HttpPostedFileBase> image)
    {
        if (image != null)
        {
            foreach (var item in image)
            {
                string filePath = Path.Combine(Server.MapPath("~/App_Data"),   Path.GetFileName(item.FileName));
                item.SaveAs(filePath);
            }
        }
        return PartialView("TestImageExtension");
    }
}

}

型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HexaPod.Models
{
public partial class ImageUpload {
  public int ID { get; set;} 
  public string ImagePath {get; set;} 
}
}

视图:

@model HexaPod.Models.ImageUpload
@{
ViewBag.Title = "Image Upload";
}
@*
*@
@using (Html.BeginForm("Create", "Profile", FormMethod.Post, new { enctype =  
"multipart/form-data", id = "frm_profile" }))
{
@Html.LabelFor(model => model.ImagePath)
@Html.TextBoxFor(model => model.ImagePath, new { type = "file", accept = "image/jpeg,  image/jpg"}) @Html.ValidationMessageFor(model => model.ImagePath)
// for multiple file select
@Html.TextBoxFor(model => model.ImagePath, new { type = "file", accept = "image/jpeg,   image/jpg", multiple = "multiple" })
}

在 ASP .NET MVC 中将图像上传到 jpg

通了!

我的方法名称是 Create(),但我在 @Ajax.ActionLink() 中没有正确表达我的方法。第二个字符串的"TestImageExtension"应该是"Create"。按钮现在呈现部分视图。