gpt4 book ai didi

c# - RESTful WCF服务图片上传问题

转载 作者:太空狗 更新时间:2023-10-29 20:22:40 25 4
gpt4 key购买 nike

1 namespace Uploader  
2 {
3 using System;
4 using System.IO;
5 using System.ServiceModel;
6 using System.ServiceModel.Description;
7 using System.ServiceModel.Web;
8 using System.Drawing;
9 using System.Drawing.Imaging;
10 using System.Net;
11 using System.Xml;
12
13 [ServiceContract(Namespace = "http://Uploader")]
14 public interface IUploaderService
15 {
16 [OperationContract, WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")]
17 bool UploadFile(string fileName, Stream fileContents);
18 }
19
20 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
21 public class UploaderService : IUploaderService
22 {
23 public bool UploadFile(string fileName, Stream fileContents)
24 {
25 return true;
26 }
27 }
28
29 class Program
30 {
31 static void Main()
32 {
33 var host = new
34 ServiceHost(typeof (UploaderService),
35 new Uri("http://localhost:8080/Uploader"));
36 host.AddServiceEndpoint("Uploader.IUploaderService",
37 new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
38 try
39 {
40 host.Open();
41 Console.WriteLine(host.BaseAddresses[0].AbsoluteUri + " running.");
42 Console.WriteLine();
43 var uri = "http://localhost:8080/Uploader/file.jpg";
44 var req = WebRequest.Create(uri) as HttpWebRequest;
45 if (req != null)
46 {
47 req.Method = "POST";
48 req.ContentType = "image/jpeg";
49 var reqStream = req.GetRequestStream();
50
51 var imageStream = new MemoryStream();
52 using (var i = Image.FromFile(@"c:\photo.jpg"))
53 i.Save(imageStream, ImageFormat.Jpeg);
54
55 var imageArray = imageStream.ToArray();
56 reqStream.Write(imageArray, 0, imageArray.Length);
57 reqStream.Close();
58 var resp = (HttpWebResponse)req.GetResponse();
59 var r = new XmlTextReader(resp.GetResponseStream());
60 if (r.Read())
61 {
62 Console.WriteLine(r.ReadString());
63 }
64 }
65 Console.WriteLine("Press <ENTER> to quit.");
66 Console.ReadLine();
67 }
68 catch (Exception ex)
69 {
70 Console.WriteLine(ex.Message);
71 Console.ReadKey();
72 }
73 finally
74 {
75 if (host.State == CommunicationState.Faulted)
76 host.Abort();
77 else
78 host.Close();
79 }
80 }
81 }
82 }
83
84

你好,希望你能帮上忙....

我正在创建一个简单的应用程序(可能是网页),它将具有简单的用户界面并从外部设备上传文件,当用户将设备插入 PC 时,应用程序/网页将通过 autorun.inf 启动。 Web 服务将执行将文件链接到管理系统等复杂工作。这将使无法使用文件浏览的 IT 文盲用户能够将文件提交到管理系统...!

我遇到的问题是,当内容类型是图像/jpeg 时,我的 RESTful 服务给我一个 400 错误。它适用于 text/plain 或 text/xml(请参阅博客文章)

谢谢J

最佳答案

您可以尝试覆盖任何内容类型并使用 IOperationBehavior 将所有文件上传为应用程序/八位字节流或文本/纯文本。

public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter
{
private IDispatchMessageFormatter innerFormatter;
public string ContentTypeOverride { get; set; }

public WebContentTypeAttribute(string contentTypeOverride)
{
this.ContentTypeOverride = contentTypeOverride;
}


// IOperationBehavior
public void Validate(OperationDescription operationDescription)
{

}

public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
innerFormatter = dispatchOperation.Formatter;
dispatchOperation.Formatter = this;
}

public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{

}

public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{

}

// IDispatchMessageFormatter
public void DeserializeRequest(Message message, object[] parameters)
{
if (message == null)
return;

if (string.IsNullOrEmpty(ContentTypeOverride))
return;

var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
httpRequest.Headers["Content-Type"] = ContentTypeOverride;
}

public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
return innerFormatter.SerializeReply(messageVersion, parameters, result);
}
}

而且您必须修改您的服务契约(Contract)以使其看起来像这样

[OperationContract]
[WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")]
[WebContentType("application/octet-stream")]
bool UploadFile(string fileName, Stream fileContents);

不过,如果您从网页上传,数据不会以 multipart/form-data 格式发布吗?

关于c# - RESTful WCF服务图片上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/664712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com