将文件从NodeJS发布到C#WCF



我目前正在设置一个nodejs服务器,但在该服务器上的操作是使用jsreport生成一个报告,将其保存在本地,然后将其发送到我们的另一个服务,即WCF。

在WCF中,接收一个Stream并将文件保存在默认目录中。

在Insomnia上测试发送文件的请求,效果非常好。但通过Node,使用Axios或Request进行通信,但生成的文件为1kb。

以下是NodeJS调用的摘录。

为了取消请求,我使用模块request,v^2.88.2。(但我也试过Axios(

const formData2 = { 
file: fs.createReadStream(reportFile) //the file path
}
request.post({
url: urlSend,//Url of post
headers: {
'Content-Type': 'multipart/form-data' 
},
formData: formData2,
}, function(error, response, body) {
console.log(response);
});

pdf文件的内部有类似的内容

----------------------------209853603972788926274398内容处置:表单数据;name="file";filename="APR-RST-06 REV-00.pdf"内容类型:application/pdf-

%PDF-1.4%东京都1 0对象<

文件发送到的C#代码(我做了一些更改,删除了其他代码(:

public string uploadFileReport(string arg1, string arg2, string arg3, string arg4, string arg5, Stream file)
{ 
try
{  
if (string.IsNullOrEmpty(arg1) || string.IsNullOrEmpty(arg2) || string.IsNullOrEmpty(arg3))
throw new Exception("Um ou mais parâmetros não foram encontrados");
string diretorioData = "C:\DATA\"; 
//Nome do arquivo.
string fileName = (string.IsNullOrEmpty(arg5) ? "relatório.pdf" : arg5); 
string directoryPath = diretorioData + "CRM\"; 
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

int length = 0;
using (FileStream writer = new FileStream(directoryPath + "\"+fileName, FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = file.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
return JsonConvert.SerializeObject(new
{
status = "OK", 
}, Formatting.Indented);
}
catch (Exception exc)
{
return JsonConvert.SerializeObject(new
{
status = "NOK",
message = exc.Message
}, Formatting.Indented);
}
}

由于某些原因,此方法需要接口文件上的UriTemplate。但是,为了工作,在节点上,我需要将这些值作为url参数发送。也许这就是原因,但正如我在《失眠》中所说,这种方法效果很好。

[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/uploadFileReport?arg1={arg1}&arg2={arg2}&arg3={arg3}&arg4={arg4}&arg5={arg5}")]
[OperationContract]
string uploadFileReport(string arg1, string arg2, string arg3, string arg4, string arg5, Stream file);

WCF默认不支持Formdata,可以使用第三方库进行正确处理,
https://archive.codeplex.com/?p=multipartparser
此外,在WCF中启用流的情况下,函数的签名不能包含多个参数
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-enable-streaming
你能正确访问服务描述页面吗?我认为这项服务不能正常工作,尽管你说你可以让它完美地工作
有关上传文件的解决方案,请参阅此链接
如何使用WCF web API通过Stream将各种类型的图像(jpg/png.gif(上传到服务器
如果有什么我可以帮助的,请随时告诉我。

对于那些可能遇到同样问题的人,以下是我的管理方法

const options2 = {
method: "POST",
url: urlSend,
headers: {
"Authorization": "Bearer " + token, 
"Content-Type": "application/octet-stream", 
},
encoding: 'binary',
multipart: [ 
{  
body: fs.createReadStream(reportFile) 
}
],
};  
request(options2, function (err, res, body) {
});

而且,在我的WCF web.config上,我添加了一个绑定,以便接受更大的文件:

<binding name="UploadBinding"
transferMode="Streamed"
maxBufferPoolSize="2000000000"
maxBufferSize="2000000000"
receiveTimeout="01:00:00"
sendTimeout="01:00:00"
maxReceivedMessageSize="2000000000" >
<readerQuotas maxDepth="2000000000"
maxStringContentLength="2000000000"
maxArrayLength="2000000000"
maxBytesPerRead="2000000000"
maxNameTableCharCount="2000000000" />
</binding>

最后,将此绑定添加到服务

<service name="WCFService.CRMH0001">
<endpoint behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="UploadBinding"
contract="WCFService.ICRMH0001" />
</service>

最新更新