在aspnet mvc中简单的图像上传 [英] simple image upload in aspnet mvc

查看:67
本文介绍了在aspnet mvc中简单的图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的学校门户网站,我一直坚持将图像上传到我的应用程序中,即用户应将学校图像上传到我的服务器,我的图像目录为./Content/Images-所有上传图像应该上传到该目录.我有以下代码

i'm building a simple school portal, i have stucked at uploading an image into my application, i.e a user should upload school image to my server, i have directory for images as ./Content/Images -- all uploading images should be uploaded to this directory. i have following code



input type="file" id="SchoolImageUrl" name="SchoolImageUrl" class="required"    

使用此获取浏览按钮,我不知道如何将图像上传到服务器,我的动作控制器将如何?我有以下创建学校的负责人

using this m'getting a browse button, i have no idea how to upload that image to server and how would be my action controller ? i have following controller for creating school



public ActionResult SchoolCreate(_ASI_School schoolToCreate, FormCollection collection)
        {
            if (!ModelState.IsValid)
                return View();
            try
            {
                // TODO: Add insert logic here
                schoolToCreate.SchoolId = Guid.NewGuid().ToString();
                schoolToCreate.UserId = new Guid(Request.Form["currentUser"]);
                schoolToCreate.SchoolAddedBy = User.Identity.Name;
                HttpPostedFileBase file = Request.Files["SchoolImageUrl"];
                file.SaveAs(file.FileName);
                //schoolToCreate.SchoolImageUrl = Reuseable.ImageUpload(Request.Files["SchoolImageUrl"], Server.MapPath("../Content"));
                //schoolToCreate.SchoolImageUrl = Path.GetFullPath(Request.Files[0].FileName);
                schoolToCreate.SchoolImageUrl = collection["SchoolImageUrl"];

                UpdateModel(schoolToCreate);
                _schoolRepository.CreateSchool(schoolToCreate);
                //_schoolRepository.SaveToDb();

                return RedirectToAction("DepartmentCreate", "Department", new { userId = schoolToCreate.UserId, schoolId = schoolToCreate.SchoolId });
            }
            catch
            {
                return View("CreationFailed");
            }
        }

在这里获取对象引用错误

here im geting object referece error

推荐答案

看看

可能是造成它的原因.您是否调试过以检查其是否为空值?

may be causing it. Did you debug to check if it's getting a null value?

这篇关于在aspnet mvc中简单的图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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