如何将图像文件从视图传输到控制器 [英] How to transfer image file from view to controller

查看:81
本文介绍了如何将图像文件从视图传输到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我使用kendoupload控件设计了kendo表单,我可以上传我的图像文件.这里我指定了一个上传按钮.当我单击该上传按钮时,图像文件必须保存到数据库并在同一页面中显示.例如,我给出当我使用fileupload:$(''#fileupload'')时,类型文件的ID就是fileupload.当我使用该语法时,我仅将路径传输到控制器,并且在控制器中将其保存到数据库中.所以我的问题是我必须将图像发送到数据库而不是路径中.所以在控制器中使用HttpPostedFileBase调用文件.因此,请帮助我如何将图像文件从视图传输到控制器,再从控制器传输到数据库.因此,请帮助我找出解决方案.

Helo,
I designed the kendo form using kendoupload control i can upload my image file.Here i specified one upload button.When i click that upload button the image file has to be saved to the database and display it in the same page.for example i give the id to the type file in the form which is fileupload.When i use fileupload:$(''#fileupload''). When i use that syntax i transfers only the path to the controller and in controller it saves it to the database.So my problem is i had to send image to the database not the path.So in controller by using HttpPostedFileBase am calling the files.So please help me how can we transfer image file from view to controller and from controller to the database.So please help me in finding out the solution.

推荐答案

(' '#上传文件'').当我使用该语法时,我仅将路径传输到控制器,并且在控制器中将其保存到数据库中.所以我的问题是我必须将图像发送到数据库而不是路径中.所以在控制器中使用HttpPostedFileBase调用文件.因此,请帮助我如何将图像文件从视图传输到控制器,再从控制器传输到数据库.因此,请帮助我找出解决方案.
(''#fileupload''). When i use that syntax i transfers only the path to the controller and in controller it saves it to the database.So my problem is i had to send image to the database not the path.So in controller by using HttpPostedFileBase am calling the files.So please help me how can we transfer image file from view to controller and from controller to the database.So please help me in finding out the solution.


希望您可以从下面的代码.
这就是我们使用的方式.

Hope you may get an idea by the below code.
This is the way what we used.

[HttpPost]
        public ActionResult uploadOrganisationImage(FormCollection collection)
        {
            System.Guid guid = System.Guid.NewGuid();
            try
            {

                string OrganisationImg = collection.Get("OrganisationImg");               
                string Filename = "";
                string FileExtension = "";
                string[] parts;
                byte[] img;
                if (OrganisationImg != null)
                {

                    parts = this.HttpContext.Request.Files[0].FileName.Split(new char[] { ''\\'' });
                    Filename = parts[parts.Length - 1];
                    FileExtension = Filename.Split(new char[] { ''.'' })[1];
                    img = new byte[Request.Files[0].ContentLength];

                    using (BinaryWriter writer = new BinaryWriter(new FileStream(Server.MapPath("../Content/" + OrganisationID + "/images") + "\\" + guid.ToString() + "." + FileExtension, FileMode.Create)))
                    {
                        Request.Files[0].InputStream.Read(img, 0, Request.Files[0].ContentLength);
                        writer.Write(img);
                    }
                    
                        orgConfig.saveOrganisationImage(long.Parse(OrganisationID), guid.ToString() + "." + FileExtension); (business logic goes here)
                }

                string xml = orgConfig.getOrgCSSConfig(long.Parse(OrganisationID));

                return this.Content(utilS.xslTransform(Server.MapPath("../Views/OrganisationConfig/xsl/ImageGrid.xslt"), xml.ToString()
                    , new string[][] { 
                        new string[] { "OrganisationID", OrganisationID.ToString() },
                        new string[] { "Time", this.Time.ToString()},
                        new string[] {"ApplicationRoleID",this.RoleID.ToString()}
                    }));
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return View();
            }
        }


这篇关于如何将图像文件从视图传输到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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