首页 新闻 会员 周边

mvc 表单提交上传附件

0
[待解决问题]

<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接受不了啊

yuxianghappy的主页 yuxianghappy | 菜鸟二级 | 园豆:225
提问于:2013-08-28 13:37
< >
分享
所有回答(2)
0

<form>中有没有加上enctype="multipart/form-data"

dudu | 园豆:30994 (高人七级) | 2013-08-28 15:44

加了啊····@using(Html.BeginForm(new{enctype="multipart/form-data"}))

支持(0) 反对(0) yuxianghappy | 园豆:225 (菜鸟二级) | 2013-08-28 16:13

@yuxianghappy: 

试试再加上@data_ajax = "false"

new { enctype = "multipart/form-data", @data_ajax = "false" }

参考:http://stackoverflow.com/a/13293284

支持(0) 反对(0) dudu | 园豆:30994 (高人七级) | 2013-08-28 16:55
0

以上传图片为例,一般是异步上传:

<input type="file"name="picFile" id="picFile" onchange="PicChange();" multiple="multiple"/>

<input type="hidden" id="picids" name="picids" value="" />

使用ajaxfileupload.js实现数据提交:

    function PicChange() {
        $("#loading").ajaxStart(function () { $(this).show(); })
    .ajaxComplete(function () { $(this).hide(); });
 
        $.ajaxFileUpload({
            url: '/Requests/PicUp',
            secureuri: false,
            fileElementId: 'picFile',
            dataType: 'json',
            success: function (data, status) {
 
    }
}
 
提交时,只需要保存图片id即可(统一存在了picids中)。
夏夏知 | 园豆:233 (菜鸟二级) | 2013-11-17 00:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册