简体   繁体   English

在ASP .NET MVC中将图片上传限制为jpg

[英]Limiting image upload to jpg in ASP .NET MVC

I'm still new to Asp .Net MVC, and C#. 我还是Asp .Net MVC和C#的新手。 I'm experiencing with learning how to do certain things and I'm running into a problem with getting the button to load my view. 我正在学习如何做某些事情,并且在获取按钮以加载视图时遇到了问题。 I learned that I don't need an event listener like I normally would in .NET MVC, so I used @Ajax.ActionLink for my button. 我了解到我不需要像通常在.NET MVC中那样的事件侦听器,因此我在按钮上使用了@ Ajax.ActionLink。 However, when I click on my button it doesn't load my view. 但是,当我单击按钮时,它不会加载我的视图。 I can't figure out why? 我不知道为什么吗? I can't make the button load my view. 我无法使按钮加载我的视图。

Here's what I have done. 这就是我所做的。 In my shared folder, I have a layout page that will include a button. 在共享文件夹中,我有一个包含按钮的布局页面。 I inserted this button: 我插入了此按钮:

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

In my controller folder I have inserted a controller with the following: 在控制器文件夹中,我插入了具有以下内容的控制器:

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");
    }

}

} }

Model: 模型:

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;} 
}
}

View: 视图:

@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" })
}

Figured it out! 弄清楚了!

My method name is Create() but I didn't word my method correctly in @Ajax.ActionLink(). 我的方法名称是Create(),但是我没有在@ Ajax.ActionLink()中正确地写出我的方法。 "TestImageExtension" should have been "Create" for the second string. 第二个字符串的“ TestImageExtension”应该为“ Create”。 Button renders partialview now. 按钮现在呈现partialview。

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

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