<div class="editor-field dform">
<input type="file" id="file" name="file2" />
</div>
[HttpPost]
[ValidateInput(enableValidation: false)]
public ActionResult Create(NewsModel model,HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
var usr = this.HttpContext.GetUserInfo();
var content = ReplaceKey(model.Content);
var entity = new News()
{
Title = model.Title,
Content = content,
CreatedBy = usr.UID,
Order = model.Ordered ? model.Order : 0,
Route = model.GetValidRoute(),
Place = model.Place,
Type = (int)NewsModel.EType.News,
};
entity.Id = NewsProvider.Instance.Create(entity);
//var path = FileUpload(file2); //上传附件
//var attname = path.Substring(path.LastIndexOf('/') + 1);
var attchement = new Attchement
{
Id = Guid.NewGuid(),
attchementId = entity.Id,
//Name = attname,
CreatedTime = DateTime.Now,
// path = path
};
var att = AttchementProvider.Instance.GetByAttchementId(entity.Id);
if (att == null)
{
var guid = AttchementProvider.Instance.Create(attchement);
}
else
{
var guid = AttchementProvider.Instance.UpdateByattchementId(attchement);
}
if (entity.Id != Guid.Empty)
{
if (model.Ordered)
{
MaxOrder = -1;
}
return Redirect("Index");
}
}
return View();
}
为什么我在后台获取的file一直是string的用HttpPost接受不了啊
<form>中有没有加上enctype="multipart/form-data"
加了啊····@using(Html.BeginForm(new{enctype="multipart/form-data"}))
@yuxianghappy:
试试再加上@data_ajax = "false"
new { enctype = "multipart/form-data", @data_ajax = "false" }
以上传图片为例,一般是异步上传:
<input type="file"name="picFile" id="picFile" onchange="PicChange();" multiple="multiple"/>
<input type="hidden" id="picids" name="picids" value="" />
使用ajaxfileupload.js实现数据提交: