【问题标题】:image File is not saving into Server "Img" Folder图像文件未保存到服务器“Img”文件夹中
【发布时间】:2016-12-07 22:07:56
【问题描述】:
[HttpPost]
public JsonResult SavePhoto(string base64)
{
    string file = "test.jpg";
    string Imgpath = Path.Combine(Server.MapPath("~/Img/"), Path.GetFileName(file));
    System.IO.File.WriteAllBytes(Imgpath, Convert.FromBase64String(base64));

    return Json(new { status= true},JsonRequestBehavior.DenyGet);
} 

我从 Postman 发出一个请求并将 Base64 字符串发送到调用 Action 它返回 "true" 并在本地保存图像,但没有将图像保存到服务器 "Img" 文件夹。如果我的代码没有问题,那么为什么 Image 没有保存到服务器 "Img" 文件夹

【问题讨论】:

  • 你对那个 IIS 用户的文件夹有写权限吗?
  • 权限怎么写...?
  • @Ganesh_Devlekar 不,它不起作用....实际上我想将图像存储到服务器文件夹“Img”中。该图像存储在我的本地计算机上,但未存储到服务器文件夹“Img”中。请帮我一个 100% 合适的答案
  • 您是否遇到任何错误或异常? 100% 的问题只是权限

标签: c# asp.net-mvc


【解决方案1】:

你可以试试这个代码,它在我的项目中运行良好

[HttpPost]
    public JsonResult SavePhoto(string base64)
    {
        string fileName = "test.jpg";
        var path = HttpContext.Current.Server.MapPath("~/Uploads/Employee/");
        string uniqueFileName = Guid.NewGuid() + "_" + fileName;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        byte[] bytes = Convert.FromBase64String(base64);
        var fs = new FileStream(path + "/" + uniqueFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        fs.Write(bytes, 0, bytes.Length);
        fs.Flush();
        fs.Close();
        fs.Dispose();

        return Json(new { status = true }, JsonRequestBehavior.DenyGet);
    }

【讨论】:

    【解决方案2】:

    检查一下

    public JsonResult SavePhoto(string base64)
        {
            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
            ms.Write(bytes, 0, bytes.Length);
            Image image = Image.FromStream(ms, true);
            string filestoragename = Guid.NewGuid().ToString() + ".jpeg";
            string outputPath = HttpContext.Current.Server.MapPath(@"~/Img/" + filestoragename);
            image.Save(outputPath, ImageFormat.Jpeg);
            return Json(new { status = true }, JsonRequestBehavior.DenyGet);
    
        }
    

    确保您已在项目解决方案中创建了 Img 文件夹

    您需要使用这些参考资料

    using System.IO;
    using System.Drawing;
    using System.Web;
    using System.Drawing.Imaging;
    using System.Web.Http;
    

    【讨论】:

    • HttpContext.Current.Server.MapPath(@"~/Img/" + 文件存储名)。有一条错误行说错误 3 'System.Web.HttpContextBase' 不包含'Current' 的定义,并且找不到接受类型为'System.Web.HttpContextBase' 的第一个参数的扩展方法'Current'(你是缺少 using 指令或程序集引用?)
    • 我已经在使用这些参考,但仍然出现错误。我添加的参考是...使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Mvc;使用共享;使用 Donation.DataLayer;使用管理模型;使用 System.Web.Security;使用 System.IO;使用 System.Drawing.Imaging;使用 System.Drawing;
    • 错误是........System.Web.HttpContextBase'不包含'Current'的定义,并且没有扩展方法'Current'接受'System.'类型的第一个参数。可以找到 Web.HttpContextBase'(您是否缺少 using 指令或程序集引用?)
    • 将该行替换为字符串 outputPath = System.Web.HttpContext.Current.Server.MapPath(@"~/Img/" + filestoragename);
    • 那么工作正常...如果您可以在 Windows 资源管理器的 img 文件夹中看到图像..那么这就是工作..但是它不会显示在您的 Visual Studio 解决方案文件夹中..否需要担心这个......即使在服务器中它也会像这样工作......如果您有疑问,只需浏览路径localhost:port/img/yourfilename.jpg
    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    相关资源
    最近更新 更多