【问题标题】:base64 encode HttpPostedFileBasebase64 编码 HttpPostedFileBase
【发布时间】:2015-09-27 13:55:36
【问题描述】:

我想将base64 编码为HttpPostedFileBase 接收的图像以将其发送到 json 对象中,但我不知道如何完成...请告诉我如何解码它回HttpPostedFileBase

【问题讨论】:

    标签: c# json asp.net-mvc web-services http


    【解决方案1】:

    我试过了,效果很好

    string theFileName = Path.GetFileName(YourFile.FileName);
    byte[] thePictureAsBytes = new byte[YourFile.ContentLength];
    using (BinaryReader theReader = new BinaryReader(YourFile.InputStream))
    {
        thePictureAsBytes = theReader.ReadBytes(YourFile.ContentLength);
    }
    string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);
    

    【讨论】:

    • 这里没有使用 theFileName
    • @SeanKPS theFileName 可以根据您的情况使用,例如,我将其用作将其存储在数据库中的 Web 服务的输入
    【解决方案2】:

    按照以下步骤将 HttpPostedFileBase 转换为 Base64String 类型

      public ActionResult ParseCv(HttpPostedFileBase cvFile)
        {            
            byte[] fileInBytes = new byte[cvFile.ContentLength];
            using (BinaryReader theReader = new BinaryReader(cvFile.InputStream))
            {
                fileInBytes = theReader.ReadBytes(cvFile.ContentLength);
            }
            string fileAsString= Convert.ToBase64String(fileInBytes);
            return Content(fileAsString);
        }
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      byte[] binaryData;
        binaryData = new Byte[product.BrochureFile.InputStream.Length];
        long bytesRead = product.BrochureFile.InputStream.Read(binaryData, 0, (int)product.BrochureFile.InputStream.Length);
        product.BrochureFile.InputStream.Close();
        string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多