Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想在ASP.NETMVC数据库中上传和下载文件。?_C#_Asp.net Mvc - Fatal编程技术网

C# 我想在ASP.NETMVC数据库中上传和下载文件。?

C# 我想在ASP.NETMVC数据库中上传和下载文件。?,c#,asp.net-mvc,C#,Asp.net Mvc,我想在ASP.NET MVC中上载和下载文件 这是我在控制器中的操作方法。当我调试文件为空时。我哪里做错了 public ActionResult UploadDocument(CompanyViewModel model,HttpPostedFileBase FileUpload) { var userId = User.Identity.GetUserId<int>(); if (model.File != null && model.File

我想在ASP.NET MVC中上载和下载文件

这是我在控制器中的操作方法。当我调试文件为空时。我哪里做错了

public ActionResult UploadDocument(CompanyViewModel  model,HttpPostedFileBase FileUpload)
{
    var userId = User.Identity.GetUserId<int>();

    if (model.File != null && model.File.ContentLength > 0)
    {
        string fileExtension = System.IO.Path.GetExtension(model.File.FileName);

        if (fileExtension == ".doc" || fileExtension == ".docx" || fileExtension == ".xlsx" || fileExtension == ".xls" || fileExtension == ".pdf" || fileExtension == ".jpeg" || fileExtension == ".jpg")
        {
            var file = model.File;
            var fileName = System.IO.Path.GetFileNameWithoutExtension(model.File.FileName);
            var contentLength = file.ContentLength;
            var contentType = file.ContentType;

            var document = new CompanyDocument();

            document.CreatedDate = System.DateTime.Now;
            byte[] byteData = new byte[file.InputStream.Length];

            file.InputStream.Read(byteData, 0, byteData.Length);

            document.FileData = byteData;
            document.FileName = fileName;
            document.CategoryId = model.CategoryId;
            document.ContentType = contentType;
            document.CreatedDate = DateTime.Now;

            _CompanyDocumentsRepository.AddOrUpdate(document);
        }
        else
        {
            TempData["ErrorMessage"] = "Only word, excel, pdf, jpg files are allowed to upload.";
            return Json(new { msgstatus = false, msg = "Only word, excel, pdf, jpg files are allowed to upload." }, JsonRequestBehavior.AllowGet);
        }
    }

    return Json(new { msgstatus = true, msg = "Document uploaded successfully." }, JsonRequestBehavior.AllowGet);
}
public ActionResult上传文档(公司视图模型,HttpPostedFileBase文件上传)
{
var userId=User.Identity.GetUserId();
如果(model.File!=null&&model.File.ContentLength>0)
{
字符串fileExtension=System.IO.Path.GetExtension(model.File.FileName);
如果(文件扩展名=“.doc”|文件扩展名=”.docx“|文件扩展名=”.xlsx“|文件扩展名=”.xls“|文件扩展名=”.pdf“|文件扩展名=”.jpeg“|文件扩展名=”.jpg”)
{
var file=model.file;
var fileName=System.IO.Path.GetFileNameWithoutExtension(model.File.fileName);
var contentLength=file.contentLength;
var contentType=file.contentType;
var document=新公司文档();
document.CreatedDate=System.DateTime.Now;
byte[]byteData=新字节[file.InputStream.Length];
file.InputStream.Read(byteData,0,byteData.Length);
document.FileData=字节数据;
document.FileName=文件名;
document.CategoryId=model.CategoryId;
document.ContentType=ContentType;
document.CreatedDate=DateTime.Now;
_公司文档repository.add或update(文档);
}
其他的
{
TempData[“ErrorMessage”]=“仅允许上载word、excel、pdf、jpg文件。”;
返回Json(新的{msgstatus=false,msg=“只允许上载word、excel、pdf、jpg文件。”},JsonRequestBehavior.AllowGet);
}
}
返回Json(new{msgstatus=true,msg=“Document upload successfully.”},JsonRequestBehavior.AllowGet);
}
我的看法是:

@model BusSchedulingSystem.Web.ViewModels.CompanyViewModel

@using(Html.BeginForm("UploadDocument","Company", null, FormMethod.Post, new { enctype="multipart/form-data"}))
{
    if (Model != null && Model.Id > 0)
    {
                    @Html.HiddenFor(m => m.Id)
                }
                @Html.AntiForgeryToken()


                    <div class="row wrapper border-bottom white-bg page-heading">
            <div class="ibox float-e-margins">
                <div class="row">
                    <div class="col-sm-6">
                        <h4 class="section-title">Upload File</h4>
                        <div class="ibox-content">
                            <div class="form-group">
                                @Html.LabelFor(m => m.FileName)

                                <input type='file' name="FileUpload" id="File">
                                <input type="submit" value="Upload" />
                                @Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })
                            </div>

                            <div class="form-group">

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

}
@model buschedulingsystem.Web.ViewModels.CompanyViewModel
@使用(Html.BeginForm(“UploadDocument”、“Company”、null、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
if(Model!=null&&Model.Id>0)
{
@Html.HiddenFor(m=>m.Id)
}
@Html.AntiForgeryToken()
上载文件
@LabelFor(m=>m.FileName)
@Html.ValidationMessageFor(model=>model.FileName,“,new{@class=“text danger”})
}

HttpPostedFileBase FileUpload是否为空值?尝试查看文件是否实际随
请求上载。文件
。检查此链接是否有帮助?