zoukankan      html  css  js  c++  java
  • vs2013 上传碰到的问题:“输入的不是有效的 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

  • 相关阅读:
    WPF程序设计 :第四章 按钮与其他控件(Buttons and Other Controls)
    C#参考 : 枚举类型
    C#3.0 新特性学习笔记(3):匿名类型
    F#语言2008年9月CTP版已经更新
    C#3.0 新特性学习笔记(1): 对象集合初始化器
    WPF程序设计基础:属性系统
    C#3.0 新特性学习笔记(2):var 隐式声明变量
    MSSql行列转换的Sql语法 详解与实例
    WPF程序设计 :第一章 应用程序和窗口(The Application and the Window)
    WPF程序设计 :第二章 基本画刷(Basic Brushes)
  • 原文地址:https://www.cnblogs.com/lovemory/p/3412932.html
Copyright © 2011-2022 走看看