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 = "未找到文件!" });
    
    }
  • 相关阅读:
    UWP AppConnection.
    Qt 多线程使用moveToThread
    C#综合细说进程、应用程序域与上下文
    C++ std::function
    商品价格加价区间的实现(策略模式)
    学习web前端三个月感悟
    triangle leetcode C++
    Linux入门视频
    轻松学习Linux之进程监视与管理
    阻止缓冲区溢出攻击
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11599094.html
Copyright © 2011-2022 走看看