上传文件时失败使用MVC 4 [英] Failure while uploading file Using MVC 4

查看:70
本文介绍了上传文件时失败使用MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVC 4上传图像。但是它会引发GMS1.dll中出现System.NullReferenceException类型的异常,但未在用户代码中处理。



我的代码如下



 <  表格    action   =    方法  = 发布    enctype   =  multipart / form-data >  

< label for = file > 文件名:< / label >
< 输入 类型 = file 名称 = < span class =code-keyword> file id = file / >

< 输入 type = 提交 / >
< / form >





 使用系统; 
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;

命名空间 GMS1.Controllers
{
public class FileUploadController:Controller
{
//
// 获取:/ FileUpload /

public ActionResult Index()
{
return 查看() ;
}

[HttpPost]
public ActionResult索引(HttpPostedFileBase文件)
{

if (file.ContentLength > 0
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath( 〜/ UserImages /),fileName);
file.SaveAs(path);
}

return RedirectToAction( 指数);
}

}
}





任何人都可以解决这个问题?任何帮助将不胜感激..提前感谢

解决方案

这些链接可能有用

http://stackoverflow.com/questions/16255882/how-to- upload-image-display-image-in-asp-net-mvc-4 [ ^ ]



http://stackoverflow.com/questions/20446580/upload-image-included-in- mvc-model [ ^ ]





 @ using(Html.BeginForm(Index,FileUploader ,FormMethod.Post,new {enctype =multipart / form-data} ))
{
< label = file > 文件名:< / label >
< 输入 type = < span class =code-keyword> file name = file id = file / >
< 输入 type = submit / >
}


I am trying to upload an image using MVC 4. But it throws an exception of type 'System.NullReferenceException' occurred in GMS1.dll but was not handled in user code.

My code is as follows

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

  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />

  <input type="submit" />
</form>



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GMS1.Controllers
{
    public class FileUploadController : Controller
    {
        //
        // GET: /FileUpload/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {

            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/UserImages/"), fileName);
                file.SaveAs(path);
            }

            return RedirectToAction("Index");
        }

    }
}



can anyone would like to solve this issue ? Any help would be grateful .. Thanks in advance

解决方案

These links may helpful
http://stackoverflow.com/questions/16255882/how-to-upload-image-display-image-in-asp-net-mvc-4[^]

http://stackoverflow.com/questions/20446580/upload-image-included-in-mvc-model[^]


@using (Html.BeginForm("Index", "FileUploader", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <label for="file">Filename:</label>
  <input type="file" name ="file" id="file" />
  <input type="submit" />
}


这篇关于上传文件时失败使用MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆