• 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 = "未找到文件!" });
    
    }
  • 相关阅读:
    Java实时读取日志文件
    Trie树的应用:查询IP地址的ISP
    vue.extend,mixins和vue.component的区别
    前端性能优化10个方面
    vue组件和插件是实现
    vue指令用法
    promise retry实现
    linux管道与重定向
    linux文件颜色与类型
    linux文件权限说明
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11599094.html
Copyright © 2020-2023  润新知