我想使用base64目录显示图像

时间:2019-05-14 07:57:55

标签: asp.net-core-2.2 asp.net-core-webapi-2.1

我想使用Asp.Net Core Web API中的base64内容显示图像。但是发生错误:

错误是:

  

PathTooLongException:路径   “数据:图像/ png格式; BASE64,iVBORw0KGgoAAAANSUhEUgAAAg0AAAFhCAYAAADzxpkoAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA + f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAzY2SURBVHja7P1LryRZtiaGfWvtbebuJ05k5KOybtW9 / bjdfQFpwIZEgALEkSYUuwH2gJKaDfAfsP + A9A8E8BfwN0iUKAjggIQ00UwDASIEtSS02EDzquvWI6sqMyPiHHc323stDdba27aZm / vxExmRGZnl1h24lefhx90ee6 / 1re9BqorbcTtux + 24HbfjdtyOpw6 + nYLbcTtux + 24Hbfjdlxz0D / 6D / 6Tta / 3AP5D // fvAvhz / 9rtuB2343bcjttxO356x1sA / WBA / X3A / WHA // G / + I // s2H5Q2tIw / 8MwL8A8L8B8B8D + MtbwXA7bsftuB2343b8pI97AP993 / F / TWD + XT / + J // 8f36paAgA / LMA / wWAv7qdv9txO27H7bgdt + NP9vgrAP / 1408米/ XP / VL / + O // yT8P5Yux + YH / NYD / 5e083Y7bcTtux + 24HbfDj1IX / K + AidPwTwH858uf / EF // // v8Y Df的+ iv84s ++ QIwMhYAIAAhE079ytEoM + gCiDKUPc0Zk5Wvk / 06 + rph95rXvQwGFQElPzlH536oCXf3Ls0 + 88iVefWdr70np3Kf4kR / 6YT7Vpev6Xe / b9tlYKpY + 1N / 90Af / FO + tlevzQ9wzt + P7vWZrv5 / zCMnjT / JeXHvd43HEV7 // BV / PF / Gv8V // N / 9va7 / 2H / 1X / + V / 9r + jf / Qf / Cc9gH8F4G + V7 / zFn3 + Jf / ZP / z383b / 7y9lK / VMtGs69VTqzUF68ODqVItq8cvmdZdFARFDVKy64 + u8RyKdKs / NNp + 9Lta / Lxn / Lp / 9p7a / bL vwLw9xnAf9QWDADwz / 7pv4e // //女士5O54ejMv7WfY6LLSyXBmCIr6MIScWi / vv6Pm3 + 0ilqsFQvTW7ktaLfjdtyO2 / Gj3ZvO7g2n / 97n6 / 69v / wF的/ TN / 4N + Y / LW / APDPIkxWWY9 // //öu7VgmKEIl1ry5o28i1nU1R / 4Geflw5hWkRUDT5wMAtXvLouD5U8WlOHpc0C1bdVrzxtdXzj8VE2 + PpZOcEKY9Nad3u6Z2 / GRXbNziPmP6f5636jE3 / VLX +如果/ U / / HfzX / 6fZqOI / JAD + R + 1X / U3 / 4X9vtsDNtiyiJ8cTdYP8AB / 2h97WtPw / AqD6xBt66t2S8RNUfYzxw56DH9tCST + iN9oW02uF9Y92k7qZyd6On0CBcfZ5 / LGtiR /克/ F7B / 4O / WhYN / 04E8Iv2Kz / 78tP16usDIg0f6Ay + 9w1KoZCyyavxNk54BfU06ZlVlla / rIUEou9vm6SCjNyQhh / 8HNyQhts9czs + zg1zrUn + qd9f177uz754tfzSLyOATfuVLsbzW1ad518 / LB + WdPK8zfuH6UQJ86KoIA / zmoAMhZjdjKe1zMk5ePJDtUXFChJ024g + 3m78dmlux + 24HR9wc / 9Qe0CMYfmlPl7abOebEsDEpwviEtpZfIi1eb6q1n / XVHqnygN98oS + z9PXqjaYeX5 + Fu9BZ3 + fFt8uRYeevtvyR + j6UcXT + 5X + yUPIP8RDt / ybP6Zu9Fnn6xlP2Y + pqP2hGOs / 1vP1U1gX2r1K1RHlj + A ++ BiRmfi8F9YP9oafgm2XqoEfy0J87cWZ5mhPFQJ0myX / BDabH / V9 + h6Xg9txO27Hj + j4QVIul1LDpxarn17Vfcl06VoB6O24FR2343bcjtvx / R5xbYP + UIvXmjPiu3Q / 3zeZbGZURde9V9vq6WxRYD8mZ7q3FYSH3lN3eDu + 983 + Vgzcjtvx42zrbk / uFUXD9402rH1tyXW49Br6zNnT + 94krtmw3 ++ mrgDoqnMD + umiFD / 0 p7rqnlPnldD1z8SHKAC / jybgJ3VvfaDPdSvuP65rfO0 + 81O8F9970fC + JCiXLsr72GwvIQ7PIrG0Aoh3uEZzruP0AvxkRgTXV7j8ficeAy3 + xm1R + jge0NXrV8y1bpflRoTEjQj5sV3j9UygudTtdh1OD / 6YHsCr3zTzx3cT4l2grOdwFtj / 3W7i23E7bsftuB0fEdJwrtqld7RxfgrOf2oscQ1S8VHJWIqfhV7fNdDVJky3zuTH0En / WHkMN / XE7bg9x7db + 1lFw / smQl4z6jjnw3CueFh7rff1vunUQ + maD1l / 9Fqy5 / vcZG4Fw8e1uZ6MzW6X53bcjh / X840bEfKdkIb3cTzluf8u6VyXUI0PVfy8rw6SPoC99e34YYuGp7Ik9Lb03I7b8dE9vz81IuT3hW4 + s2h4jkxy / YO0Xdj7umjMfNLdicgHq0DP / xd9hw39HZI + r03G / IEr9lvXcnsHt + N2fOD29Il7nVZ + 9tJr3Qr9q4uGS13T9Zv7u + nWrzF6epe8hbVRgK4FQjyz0nuKe3H9 + aIPEg / + obaKayva814VP / 498EPZLX + YbuUDfa6b5PKjeN3bsdzk6eKiMb / Fl1EG4v / SND +侯+ NDH / EJ + rSv0e4qKAPywv8Y4CkbsftuB2343Z8H8U0LUaNTWN5O0XXFQ3nuAc / ZKFwDbpw7e / fjttxO27H7fhTLhaKKeDTKNw5btwlTtNP / eCf8o3BzB / MZe923I7bcTtux0 + 4uLidgtXjoxlPrBEkvwtqsMbFaMmRf8qV4u24HbfjdvxJFwR1 / DDN / CyeRB1 + 6D30By8avq8u / X3CPz91VOHHZEv8wYiQt + MjW3hvx + 34uFadS7jB5f1EZ0R5WrnnP9Ym8 / t6Hj9aIuTahXlKdXHNSb2hCrfjdtyO23E7bsd7LBp + zB3Emi / ErTO6HbfjdtyO2 / E + MSC / 9ebzWeqJD + mw + L7VD5eyL25ow + 24HbfjdtwKh + / y + 99lf / rJIQ0 / xMVbEiHXLuqlYuZdsxxuBcTtuB2343b8aRYOy71g5vmH67OT / pT2kSeLhnc1Q9LZqb / 8 / XMN / PTLdPa / L / 3safGx / Dn9oDfjx3asFVm343b8GO7d23E7PuS9TzTfH25N5TOKhu9yshQEvcbeu / n59SLg1Jb5wyxp72ZmVTbfpf31hxznvM / C4V03i48hPvmnurl9iHvmVgjcjj + hrR9PqScuNcRlPa / owY + oaPgo1BPLMcGHelPMfNXJoO9wMi / zGujqi3HNov4uC // VhQq9n5vrVkHfjttxO / 7UkYXb8R6Khh9yQ / MQF / Ey0fL8iILeMczqx / DA3AqH23E7bsftmCMSE3pM + DH5Qn5f6znfbpLbcTtux + 24HbfjdlxzfFQ + DD / VvOn7qLrW7KnPRWI / X / L6Q0RjP3U + PvR1JtwgwO9yf92O23E7vr9n7oa8foei4Z03tHZfewaZsGRCPJVd / hwi5PNugOuLlTX + xbskb / 4QD8ZP6aG4ESFv5 + p23I4zrcqT3da5Jmqpnritic8sGlom6Y + xEruWwPmc76 / 97Pu4WNe / xrvnvF8idD7nM9wq8ttxO27Hrem4IQ0fzQU827l / oJuE + bsVDbfjdtyO23E7bsefdNHQzus / JNLwnM6dvsNrXxohEOl3fm8 / xkr7hhjcjttxO27HfJe5qSc + cqRhVpSUzv8St + HKE3PO + 2EN1XhO0fCDX / D3yOv48GMnuiEz76GIvh2343bcjo8WaWjXrAltsP8OwZweyyazNEwqRJLn4AKrRMv3UDR8F2TjqcyLD7EBrP3s6t / Vs7XE6qZ9ZR3yLJ7E1VHkz6zSn3OeP8zm + V7 / vkKh8sTPkr7D + yEw8Xs / r3jWNXjWu53uR7rwHqn9b71QjAIAg + T0Ne17c0M4UYGqXDzH9S2wTr + o56zqaWX90pXX1Gef5rU18TStd / qbIvmK5uh5awKetdRevvLL5 + H7e8bpulWKAEDsvVG5T + R5z / kHWrs + RjR4 tWgoXfrs4fOH1EI9BCJ68nsh8POVFs9QZ3zfrp5rxdGHRFsuFStPkzrpHUsG + 8kP8jDrj5E0 + QHe71ObO8n38z7e9ylYFDt0afFuN9 + nfots056Zrun043VZUgKUTzZ3c5 / R +的eZ + FmqW03dEOl0yLZb4ipKTQ8SYLG50toHPX3d5Uunqp5OIqqpsfQ1iMJNvcHrVM77 + vs4dfP36ccmmlq7qbj7k6nr9vVyLBf0OdPM / QaSBma / gAjCY9QzS8DzI + zmbyofagK6F8T / Ueygpn9cUDR + QY / 4YYPEfEzR // X1ATrR9xsJaNxm9XJDJB + AY1V35 + dXF2eungLT38pMVijao5To6oVDQ7DWbDVamc1d + IjCvE55pysmh2camKx16WeMm1ERnhbGe / Uyq89d66l4XkbPPf9n8Raieq + VXR + u32g / iHUMf4hn7MM / 3dwsvuBUNJ93ucpM7Cfr4ER / VA / P6qcoYnwXF / egKjPcNRxLonbqWK0hY9P4LcyI + U / jravNQv0cXX / RM4aQnzaqNcxRKxm0iItttm7GFiEHJRAqO7Wao9TWml1ffYtOZ2kxWzo + u7P8029CX697J + cCaD8D199gyifa0adCmsOGVgkBX3t + zYKTr79paddEP9oyv3 + PtdeAZknR6LnTl924Fw9VFw7mL1t7If2qErQ9NGnzqfDLzTe3w4a / YB / NZY / N54KNCQpt5OdUuXK44B0 + gdVwg / eWMcdHml / 1adTZg0OXmoF5o0Hy / Ipon7KoPJvQcp4H1pPayTdgLEKGVIo5m6 + H07NLZosFMg959BBhCuOrSqa5dm3dw1MXzEWBauSVW95Bn3YsfvAX6yB / IH0HR8K4b3YeGz38IZOEay + J3 + R6W53NN6bE2xvi + 0YPb8bz1bJ2A991JXj / kc3Zyv + h5tEGhYM11Mz2F8xukQQ1pKJuslPuepmJiKhoykuhsoz4lVRvWY1 / W011Nr0F62rGEd + FaNsAzLGJabziuXSff tcsmek / Ptr6Ldf8a + ZneeUt + VglPa8Xd2qs99dcVt6XwmUVDu1ldutnObWg / XOB + ff6972vkcOl8Xq20eO4T / 5Feh + 9tI6QPcC / qfPY + Xzmv / YO8WrU8y0X0ao7R + S / Xvfop4h3p + t8rhRNNXIy255TFH1rO8wuqQDMURDDqeN1zu3K6L6ukQiVZEhFCiGAOk3JB6QKCpADpiapiee4uG87xO60zayPiUqwQ0TNe9znPuJ7dE5Zft9HSu2MA536SkN8L6jDd5 / qjW + T + UBvpaxaY55AHf6zd8Nrf + 1DEzZ9qp / 9dZaff8wW / vgt6Bkls / WefYxyz3q21s / X3eh + eEPCoURLls5uE / aicFA3UoiI6jWtUeb6xoXAW1McJ60UD + TlVCETGimbMAQQ9KULa12w3U9tQJ1Kj / WNAA5gZIRBi7GvhwE6EnBqsqTiofANXgLSd94R2PI1cisiT99gsk2dRRLaoZOGBPO9ZDO / wPNKJZP + kkHhOMSTyIVak1VHRd1XmfUgE + mPbQ + J3qzB / Mj3mBzn5t / Ch7的+ 2Sfe8verVygRS0Np7QtaLh3LWVk59RVYjmq + / D61nzc8h + KTtuu + V1LwHbME + KhhCc0dAohVRqoVCKBlFFSskKuFLElaKhvBaz00vVVBMz9KJVIk3vw9QIRSquJ0VDKY4ARc7iwAiDOaLrArpO0HU9QujAvqFOr9XK0oui4fR55bDug7N2Lssmfz7ET + p7DiGcENjbayIi9fX0anM8PEsKXz6 / 6tyrZ / 7fz9th9J0xCV35S7eZwwcrGoj4J9 / 9rm / U5 + RN6zDt91EM0Aqm + jFck + / HdOWJVUp / 6Hvm2i5IoZKxrtM / LRpUz90H5eXs + RSVZxYNevWVIawXDdNzss6Wb78 / + 3sEZM3IXrxMXf / 03 + LXVKHIOV84m9M7VAAZ ='   太长,或指定路径的组成部分太长。

Web Api控制器:

private string ReadFileInBase64(string extension, string fileName)
    {
        using (FileStream fs = new FileStream(
            fileName,
            FileMode.Open,
            FileAccess.Read,
            FileShare.Read))
        {
            using (var streamReader = new MemoryStream())
            {
                fs.CopyTo(streamReader);
                byte[] fileContents = streamReader.ToArray();

                string base64MimeFileContents = ConvertToBase64(extension, fileContents);

                //(string MimeType, string ParsedBase64Contents) = GetBase64MimeType (base64MimeFileContents);

                return base64MimeFileContents;
            }
        }
    }

    private string ConvertToBase64(string extension, byte[] fileContents)
    {
        string base64contents = Convert.ToBase64String(fileContents);

        base64contents = String.Format($"data:image/{extension.ToString()};base64,{base64contents}");

        return base64contents;
    }

    public class file
    {
        public string extension { get; set; }
        public string fileName { get; set; }
    }


    [HttpPost]
    [Route("File")]
    public string Getfile(file f)
    {
        string extension = f.extension;
        string fileName = f.fileName;
        //string path = _hostingEnvironment.WebRootPath + "/images/" + fileName;
        string path= ReadFileInBase64(extension, fileName);
        byte[] b = System.IO.File.ReadAllBytes(path);
        return "data:image/png;base64," + Convert.ToBase64String(b);
    }

0 个答案:

没有答案