vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”

本文介绍了一个ASP.NET MVC应用程序中实现图片上传功能的具体代码示例,并针对上传过程中出现的“输入的不是有效的Base-64字符串”错误进行了分析与解决。

action 代码:

      [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(  ImageStoreModels imagestoremodels)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files["ImageData"].ContentLength <= 0) return View();

                imagestoremodels.ID = Guid.NewGuid();

                HttpPostedFileBase aFile = Request.Files["ImageData"];
                int contentLength = aFile.ContentLength;
                byte[] bytePic = new byte[contentLength];
                aFile.InputStream.Read(bytePic, 0, contentLength);
                imagestoremodels.ImageData =  bytePic;
                imagestoremodels.ImageName = aFile.FileName;
                //imagestoremodels.ImageCataloge = e_ImageCataloge.;
                //imagestoremodels.ImageAlt = "";
                imagestoremodels.ContentType = aFile.ContentType;


                db.ImageStoreModels.Add(imagestoremodels);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(imagestoremodels);

        }
action 上传代码

view 代码

@using (Html.BeginForm("Create", "ImageStore", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>ImageStoreModels</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.ImageCataloge, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                 
                @Html.DropDownListForEnum(model => model.ImageCataloge)
                @Html.ValidationMessageFor(model => model.ImageCataloge)
            </div>
        </div>

   

 

        <div class="form-group">
            @Html.LabelFor(model => model.ImageData, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.TextBoxFor(model => model.ImageData, new { type = "file",  style="none"})*@
                 
                <input name="ImageData" type="file"  />  
                @Html.ValidationMessageFor(model => model.ImageData)

                <input name="ImageData" id="ImageData" type="file" />
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.ImageAlt, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ImageAlt)
                @Html.ValidationMessageFor(model => model.ImageAlt)
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
View 代码

提交图片, 上传的时候,总是报: 输入的不是有效的 Base-64 字符串    的错误。

 

后来,在网上才找到答案:

 

http://stackoverflow.com/questions/3294023/input-file-autobind-to-byte-aray-in-asp-net-mvc

 

 

 

转载于:https://www.cnblogs.com/lovemory/p/3412932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值