Home  >  Article  >  Backend Development  >  Advertising space management under the MVC+WCF framework - file upload

Advertising space management under the MVC+WCF framework - file upload

黄舟
黄舟Original
2017-02-23 09:50:441413browse

Advertising space is one of the essential contents of the website, and it is also one of the contents that can directly bring economic benefits to our website. A good advertising space will not only not overpower the guest, but will also add to the beauty of our website and play the finishing touch. Therefore, designing a good advertising space is also an important step in the development process.
I am currently working on a jewelry testing station project, and the management page involves advertising space management. There were many advertising spaces in previous projects, but I was not responsible for them. The general idea is to upload the corresponding picture from the management page, save it to the database, and then select one from all uploaded pictures to display on the homepage of the website. The principle is not too difficult, but due to performance issues, we only need the path to save the image in the database.
The point is that under a framework like MVC+WCF, it may be a little difficult. After sharing with online bloggers and communicating within the team, the most basic functions of uploading and saving the database have been achieved. There are five advertising spaces on the homepage of the website. Due to the different locations and sizes of the advertising spaces, of course the charging standards are also different, haha~~ We initially designed each advertising space to have a separate management unit. My specific implementation is the management of advertising spaces. There are five Datagrads on the page, and each table is used to display the image path and corresponding information of this type of advertising space saved in the database.

As shown below:
Advertising space management under the MVC+WCF framework - file upload

All data in the database is displayed in the table, by operating the radio button of whether to display the column. Determines which ad image is displayed first. (You need to name the name attribute of the radio buttons uniformly, and set the radio buttons of the entire table as a group)
What we are focusing on here is file upload. Why only save the path in the database instead of directly saving the image? First of all, this is what most developers do, and secondly, to a certain extent, saving paths has higher performance than images. I actually implemented it in MVC, so I won’t say much and will go directly to the code.

View

<h2>上传文件</h2>
@using(Html.BeginForm("FileUpLoad","AdvertisementManage",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<br />
@*输入赞助商名称*@<input type="text" name="AdvertisementName" />
@*上传文件*@<input type="file" name="file" />
@*提交*@<input type="submit" name="UploadFile" />
}

Controller

        public  ActionResult FileUpLoad()
        {
            HttpPostedFileBase file = Request.Files["file"];//获得上传文件

            //判断文件内容是否为空
            if (file != null)
            {                string filePath = Path.Combine(HttpContext.Server.MapPath("../images"), Path.GetFileName(file.FileName)); //设置文件保存路径
                file.SaveAs(filePath);//将文件保存到filePath路径下

                Guid g = new Guid("6dc3f7db-f038-4c48-9564-0ac52e0e29c1");                //实例化viewmodel,给属性赋值
                AdvertisementManageViewModel advertisement=new AdvertisementManageViewModel();
                advertisement .AdvertisementID=System.Guid.NewGuid();
                advertisement.AdvertisementName = Request.Form["AdvertisementName"];
                advertisement.AdvertisementUrl = filePath;
                advertisement.TimeStamp = DateTime.Now;
                advertisement.UserID = g;
                advertisement.IsEnable = 0;                var service = ServiceFactory.GetService();//声明WCF服务
                var s = service.AddAdvertisement(advertisement);//调用服务端的添加方法,将广告内容保存到数据库


                //如果保存成功,返回FileUpLoad视图
                if (true)
                {                    return RedirectToAction("FileUpLoad", "AdvertisementManage");
                }

            }            else
            {                //return Content("<script>alert(&#39;上传失败!&#39;)</script>");
                return View();
            }
        }

Rendering
Advertising space management under the MVC+WCF framework - file upload

##The above are the functions currently implemented , both style and logic need to be optimized. I would like to share the simple principles with everyone. Please criticize and correct any shortcomings.

Advertising space is one of the essential contents of the website, and it is also one of the contents that can directly bring economic benefits to our website. A good advertising space will not only not overpower the guest, but will also add to the beauty of our website and play the finishing touch. Therefore, designing a good advertising space is also an important step in the development process.

I am currently working on a jewelry testing station project, and the management page involves advertising space management. There were many advertising spaces in previous projects, but I was not responsible for them. The general idea is to upload the corresponding picture from the management page, save it to the database, and then select one from all uploaded pictures to display on the homepage of the website. The principle is not too difficult, but in view of performance issues, we only need the path to save the image in the database.
The point is that under a framework like MVC+WCF, it may be a little difficult. After sharing with online bloggers and communicating within the team, the most basic functions of uploading and saving the database have been achieved. There are five advertising spaces on the homepage of the website. Due to the different locations and sizes of the advertising spaces, of course the charging standards are also different, haha~~ We initially designed each advertising space to have a separate management unit. My specific implementation is the management of advertising spaces. The page has five Datagrads, and each table is used to display the image path and corresponding information of this type of advertising space saved in the database.

As shown below:


Advertising space management under the MVC+WCF framework - file upload

#All data in the database is displayed in the table, and you can determine which one to display first by operating the radio button of whether to display the column. Ad Image. (You need to name the name attribute of the radio buttons uniformly, and set the radio buttons of the entire table as a group)

What we are focusing on here is file upload. Why only save the path in the database instead of directly saving the image? First of all, this is what most developers do, and secondly, saving paths has higher performance than images to a certain extent. I actually implemented it in MVC, so I won’t say much and will go directly to the code.

View

<h2>上传文件</h2>
@using(Html.BeginForm("FileUpLoad","AdvertisementManage",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<br />
@*输入赞助商名称*@<input type="text" name="AdvertisementName" />
@*上传文件*@<input type="file" name="file" />
@*提交*@<input type="submit" name="UploadFile" />
}

Controller

        public  ActionResult FileUpLoad()
        {
            HttpPostedFileBase file = Request.Files["file"];//获得上传文件

            //判断文件内容是否为空
            if (file != null)
            {                string filePath = Path.Combine(HttpContext.Server.MapPath("../images"), Path.GetFileName(file.FileName)); //设置文件保存路径
                file.SaveAs(filePath);//将文件保存到filePath路径下

                Guid g = new Guid("6dc3f7db-f038-4c48-9564-0ac52e0e29c1");                //实例化viewmodel,给属性赋值
                AdvertisementManageViewModel advertisement=new AdvertisementManageViewModel();
                advertisement .AdvertisementID=System.Guid.NewGuid();
                advertisement.AdvertisementName = Request.Form["AdvertisementName"];
                advertisement.AdvertisementUrl = filePath;
                advertisement.TimeStamp = DateTime.Now;
                advertisement.UserID = g;
                advertisement.IsEnable = 0;                var service = ServiceFactory.GetService();//声明WCF服务
                var s = service.AddAdvertisement(advertisement);//调用服务端的添加方法,将广告内容保存到数据库


                //如果保存成功,返回FileUpLoad视图
                if (true)
                {                    return RedirectToAction("FileUpLoad", "AdvertisementManage");
                }

            }            else
            {                //return Content("<script>alert(&#39;上传失败!&#39;)</script>");
                return View();
            }
        }

Rendering


Advertising space management under the MVC+WCF framework - file upload## The above is the advertising space management under the MVC+WCF framework— —File upload content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:ASPX and RazorNext article:ASPX and Razor