zoukankan      html  css  js  c++  java
  • web api文件上传

    public IHttpActionResult ConfirmUpload()
    {
    HttpFileCollection files = HttpContext.Current.Request.Files;
    if (files != null)
    {
    try
    {
    foreach (string key in files.AllKeys)
    {
    if (files[key].FileName != "")
    {
    HttpPostedFile file = files[key];
    //string exName = Path.GetExtension();
    string uploadPath = ConfigurationManager.AppSettings["FoodPath"].ToString();
    if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadPath)))
    {
    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadPath));
    }
    #region 大图保存
    var fileName =Path.GetFileName(file.FileName);//解决IE和Google中file上传(是否带本地物理路径)
    string pattern = "[\[ \] \^ \-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,;"‘’“”-]";//过滤特殊字符
    fileName = Regex.Replace(fileName, pattern, "_");
    
    var filePath = (uploadPath + fileName).Replace(@"/", @"");
    string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString());
    string fileSavePath = tmpRootDir + filePath;
    //文件存在即删除
    if (File.Exists(fileSavePath))
    {
    File.Delete(fileSavePath);
    }
    file.SaveAs(fileSavePath);
    
    return Json(new { path = filePath, success = true, content = "上传图片成功!" });
    }
    }
    }
    catch (Exception e)
    {
    return Json(new { success = false, content = e.Message });
    }
    }
    return Json(new { success = false, content = "未找到文件!" });
    
    }
  • 相关阅读:
    389 Directory Server RHEL 6.0以上客户端安装方法
    389 Directory Server RHEL 6.0以下客户端安装方法
    堆排序算法
    单链表倒置(递归与非递归)
    发帖水王问题及扩展
    2.5 k个最大的数
    2.7 最大公约数
    进制转化的算法问题。
    boost图库简单操作
    C++编程必备
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11599094.html
Copyright © 2011-2022 走看看