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

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

程序员文章站 2022-06-30 12:17:45
/// 文件图片上传 /// /// 成功上传返回上传后的文件名 [HttpPost] public async Task UpLoadImageFiles() { var files = HttpContext.Current.Request.Files; ... ......
   /// 文件图片上传 
        /// </summary> 
        /// <returns>成功上传返回上传后的文件名</returns>
        [httppost]
        public async task<ihttpactionresult> uploadimagefiles()
        {

            var files = httpcontext.current.request.files;
            if (files.allkeys.any())
            {
                using (httpclient client = new httpclient())
                {
                    httpcontextbase httpcontext = (httpcontextbase)request.properties["ms_httpcontext"];
                    var request = new httprequestmessage(httpmethod.post, configurationmanager.appsettings["savetoimgaes"].tostring() + "/upload/");
                    var content = new multipartformdatacontent();
                    content.add(new streamcontent(httpcontext.request.files[0].inputstream), "file", "file.jpg");
                    request.content = content;
                    var response = await client.sendasync(request);
                    response.ensuresuccessstatuscode();
                    var filenamestr = await response.content.readasstringasync();//结果
                }
            }
            return ok();
        }