zoukankan      html  css  js  c++  java
  • 学习日记18、easyui 文件框上传文件

    前台

    <tr>
    <td style="100px; text-align:right;">
    @Html.LabelFor(model => model.ImgUrl):
    </td>
    <td style="310px">
    @Html.HiddenFor(model => model.ImgUrl)
    <input class="easyui-filebox" name="File" id="filed" data-options="prompt:'请选择..'" style="200px;display:none" />
    </td>
    <td>@Html.ValidationMessageFor(model => model.ImgUrl)</td>
    </tr>

    前台提交的时候需要easyui   from表单的异步提交方法(别的提交表单方法后台获取不到数据,不知道为什么)

    提交表单代码:

    $("#btnSave").click(function () {
    if ($("form").valid()) {
    $("input[name='Content']").val(editor.getContent())
    $("#ZxdFm").form('submit', {
    datatype: 'json',
    success: function (data) {
    var json = eval("(" + data + ")");
    if (json.type == 1) {
    window.parent.frameReturnByMes(json.message);
    window.parent.frameReturnByReload(true);
    window.parent.frameReturnByClose()
    }
    else {
    window.parent.frameReturnByMes(json.message);
    }
    }
    })
    }
    return false;
    });

    这个返回回来的数据需要用eval解析一下,直接用获取不到数据

    public JsonResult Create(X_ZxDynamicInfoModel model,HttpPostedFileBase File)
    {
    model.Id = ResultHelper.NewId;
    model.CreateTime = ResultHelper.NowTime;
    if (File != null)
    {
    string imgurl = UploadFile(File);
    if (imgurl == "1")
    {
    return Json(JsonHandler.CreateMessage(0, "请选择正确的图片格式"));
    }
    model.ImgUrl = imgurl;
    }

    }

    后台接受文件代码,UploadFile(HttpPostedFileBase  File)是我封装的一个方法,返回的是一个图片文件路径

    方法代码如下

    public string UploadFile(HttpPostedFileBase File)
    {
    string FileExtName = System.IO.Path.GetExtension(File.FileName).ToLower();
    switch (FileExtName)
    {
    case ".jpg":
    case ".jpeg":
    case ".png":
    case ".gif":
    break;
    default:
    return "1";
    }
    //保存位置
    string SaveFile = DateTime.Now.ToString("yyyyMMddHHmmssff");
    //保存扩展名
    SaveFile += System.IO.Path.GetExtension(File.FileName);
    //生成完整的存储路径(网址)
    string SavePathFile = string.Format("/Uploads/{0}", SaveFile);
    string SavePathUrl = SavePathFile;//记录保存的url路径
    //转化生成磁盘物理路径
    SavePathFile = Server.MapPath(SavePathFile);
    //开始保存
    File.SaveAs(SavePathFile);
    return SavePathUrl;
    }

    其他的文件上传也应该这样,因为机制都是差不多的

  • 相关阅读:
    self 和 super 关键字
    NSString类
    函数和对象方法的区别
    求两个数是否互质及最大公约数
    TJU Problem 1644 Reverse Text
    TJU Problem 2520 Quicksum
    TJU Problem 2101 Bullseye
    TJU Problem 2548 Celebrity jeopardy
    poj 2586 Y2K Accounting Bug
    poj 2109 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/Wxinchun/p/8871736.html
Copyright © 2011-2022 走看看