当我提交表格时为什么给我空虚(上传图片) [英] Why Give Me Null When I Submit My Form (Upload Image)
本文介绍了当我提交表格时为什么给我空虚(上传图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
i想把我的照片上传到我的主人并将url'picture保存到数据库
在我的数据库表中我有这个文件:
id
title
imageurl
我写这个行动代码:
hello
i want to upload my picture to my host and save url'picture to database
in my table on database i have this fileds:
id
title
imageurl
and i write this code for action:
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(HttpPostedFileBase ImageFile, TNew tnew)
{
if(ImageFile!=null && ImageFile.ContentLength>0)
{
var file = Path.GetFileName(ImageFile.FileName);
var pathfile = Path.Combine(Server.MapPath("~/Photo"), file);
ImageFile.SaveAs(pathfile);
tnew.imageurl = pathfile;
_db.TNews.Add(tnew);
_db.SaveChanges();
}
return View();
}
这个用于创建页面:
and this for create page:
@using (Html.BeginForm("Create", "HOME", FormMethod.Post, new {enctype="mulipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>TNew</legend>
<div class="editor-label">
@Html.LabelFor(model => model.title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.imageurl)
</div>
<div class="editor-field">
<input type="file" name="ImageFile" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
but when i submit my form image file is null
how can i solve this?
推荐答案
你在表格标签中是拼写错误的enctype,这就是发布文件永远为空的原因
你的代码
You are Mispelled enctype in Form Tag that's why posted file always be null
Your Code
@using (Html.BeginForm("Create", "HOME", FormMethod.Post, new {enctype="mulipart/form-data" }))
更正后的代码
Corrected Code
@using (Html.BeginForm("Create", "HOME", FormMethod.Post, new { enctype = "multipart/form-data" }))
希望这有助于
Hope this helps
这篇关于当我提交表格时为什么给我空虚(上传图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文