【发布时间】:2021-02-06 07:21:08
【问题描述】:
我目前正在尝试将一些 aspx 页面迁移到 C# Rest 服务中。 我不能改变调用者,只能改变被调用的服务。
我是这样称呼的:
var myWebClient = new WebClient();
// here I'm dealing with cookie but not sure it is worth explaining how I pass it cause it isn't the problem
var response = myWebClient.UploadFile(sURL, fileFulName); // fileFulName is the path of my local file
在 aspx 中,文件可以这样获取:
HttpPostedFile file = Request.Files[0];
首先我尝试了同样的代码:
var file = HttpContext.Current.Request.Files[0];
这会引发我可以通过将其添加到 web.config(appSettings 部分)来管理的异常:
<add key="wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode" value="true" />
但是现在,它没有抛出异常,而是一个空数组。 这是我的端点方法:
[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/upload?myParam={myGetParam}]
void Upload(Stream fileStream, string myGetParam);
我不知道如何让它工作。我尝试使用字节数组作为输入,但响应是 400 Bad Request。然后我尝试查看 fileStream 中的内容,但它不包含我的文件,只有几个字符。
我不知道如何获取上传的文件。
【问题讨论】: