如何使用 WCF Web API 通过 Stream 将各种类型(jpg/png/gif)的图像上传到服务器

编程入门 行业动态 更新时间:2024-10-28 08:30:21
本文介绍了如何使用 WCF Web API 通过 Stream 将各种类型(jpg/png/gif)的图像上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

出于某种原因,我将 WCF 用于 webAPI.我已经使用 WCF 好几天了.在寻找将图像上传到服务器的代码的过程中,我找到了许多答案和解决方案,例如:

正文部分将发布二进制数据,就像您一样.另外,WCF默认不支持form-data,不过我们可以利用第三方库的方式,传递form-data参数.最后,内置支持在 Asp WebAPI 中传递表单数据.docs.microsoft/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2如果有什么我可以帮忙的,请随时告诉我.

For some reason, Im using WCF for webAPIs..I have been using WCF for few days now. On the hunt to find the code to upload image to the server I found many answers and solutions like:

Could not upload Image to WCF Rest service

Uploading image as attachment in RESTful WCF Service

Uploading an image using WCF RESTFul service full working example

the above (full woking) example is working for me..accept for the part that it accepts only jpeg images. Since im using Postman to hit on the request, Stream datatype is accepting it and run to the program. Is there anyway to get a file type or file name from input stream data?

Following is the code

interface: Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PostImage(Stream stream); method implementation: public string PostImage(Stream stream) { byte[] buffer = new byte[10000]; stream.Read(buffer, 0, 10000); FileStream f = new FileStream("C:\\temp\\sample.jpg", FileMode.OpenOrCreate); f.Write(buffer, 0, buffer.Length); f.Close(); return "Recieved the image on server"; }

PS: im using postman request to send image like the following simple browse file option under byte section like this:

enter image description here

PS: Along with the image, I cannot pass the file name if even i want to, since Stream parameter does not allow any other parameter with it.

解决方案

As joehoper mentioned, presently we could pass the additional information to the server by using Http header, please refer to my code design. Server-side. Service interface.

[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Bare)] Task UploadStream(Stream stream);

Service implementation.

public async Task UploadStream(Stream stream) { var context = WebOperationContext.Current; string filename = context.IncomingRequest.Headers["filename"].ToString(); string ext = Path.GetExtension(filename); using (stream) { //save the image under the Uploads folder on the server-side(root directory). using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() + ext))) { await stream.CopyToAsync(file); } } }

Client-side. The body section will post the binary data, just like you do. Besides, WCF doesn’t support the form-data by default, while we could take advantage of the third-party library, which enables us to pass the form-data parameter. archive.codeplex/?p=multipartparser Then we could post form-data to transfer the file information. Please refer to the below code.

public async Task UploadStream(Stream stream) { MultipartParser parser = new MultipartParser(stream); if (parser.Success) { //absolute filename, extension included. var filename = parser.Filename; var filetype = parser.ContentType; var ext = Path.GetExtension(filename); using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() +ext))) { await file.WriteAsync(parser.FileContents, 0, parser.FileContents.Length); } } }

Finally, there is built-in support for passing the form-data in Asp WebAPI. docs.microsoft/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2 Feel free to let me know if there is anything I can help with.

更多推荐

如何使用 WCF Web API 通过 Stream 将各种类型(jpg/png/gif)的图像上传到服务器

本文发布于:2023-11-03 19:50:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   类型   服务器   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!