欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# API接受图片文件以Base64格式上传图片

程序员文章站 2022-04-24 10:05:44
/// base64上传图片 /// /// 成功上传返回上传后的文件名 [HttpPost] public async Task UpLoadImageBase64() { HttpContextBase context = (HttpContextBase)Request.Prope... ......
 /// base64上传图片
        /// </summary> 
        /// <returns>成功上传返回上传后的文件名</returns>
        [httppost]
        public async task<ihttpactionresult> uploadimagebase64()
        {
            httpcontextbase context = (httpcontextbase)request.properties["ms_httpcontext"];
            string text = context.request.form["file"];

            stream stream = new memorystream(convert.frombase64string(text.split(',')[1]));
            using (httpclient client = new httpclient())
            {
                var request = new httprequestmessage(httpmethod.post, configurationmanager.appsettings["imgaes"].tostring() + "/upload");
                var content = new multipartformdatacontent();

                //client.defaultrequestheaders.add("fileext", httpcontext.request.headers["fileext"]);
                content.add(new streamcontent(stream), "file", "file.jpg");
                request.content = content;
                var response = await client.sendasync(request);
                response.ensuresuccessstatuscode();
                var filenamestr = await response.content.readasstringasync();//返回结果
            }
            return ok();
        }