gpt4 book ai didi

javascript - Dropzone JS 上传到 WCF 获取错误数据

转载 作者:搜寻专家 更新时间:2023-11-01 04:29:58 25 4
gpt4 key购买 nike

首先,我对 WCF 服务和 dropzone JS 都不熟悉,但我正在尝试将两者结合起来创建一个简单的图像 uploader 。我的 WCF 可以正确处理我通过它上传到的元数据(所以我知道它正在正确地跨域传递内容),但是我从 Dropzone 捕获的流与我删除的图像不匹配.事实上,丢弃的每个图像都会在服务器端产生相同的编码字符串......

例如,我使用了这个 star image作为测试,并将图像上传到 base64 online converter ,我可以看到 base64 字符串的开头如下所示:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAADbCAMAAABOUB36AAAAwFBMVEX...

但是,当我调试 WCF 代码时,base64 转换后的字符串如下所示:

LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5T1RUV0I1RFZZdTVlM2NTNQ0KQ29udG...

上面的字符串与为我尝试发送的每张图片创建的字符串相同。

现在所有适用的代码片段。我在一个项目中有一个简单的网页,在同一解决方案的另一个项目中有 WCF 相关文件。

索引.html:

<div class="col-lg-12">
<form action="http://localhost:39194/ImageRESTService.svc/AddImageStream/"
class="dropzone"
id="dropzone"></form>
</div>
...
Dropzone.options.dropzone = {
maxFilesize: 10, // MB
};

操作合约:

/*  Stores a new image in the repository  */
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "AddImageStream/")]
void AddImageStream(Stream img);

AddImageStream 实现:

public void AddImageStream(Stream img)
{
//try to save image to database
byte[] buffer = new byte[10000];
int bytesRead, totalBytesRead = 0;
string encodedData = "";

do
{
bytesRead = img.Read(buffer, 0, buffer.Length);
encodedData = encodedData + Convert.ToBase64String(buffer,
Base64FormattingOptions.InsertLineBreaks);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
}

Webconfig适用件:

<services>
<service name="ImageRESTService.ImageRESTService" behaviorConfiguration="serviceBehavior">
<endpoint address="" behaviorConfiguration="web" contract="ImageRESTService.IImageRESTService" binding="webHttpBinding" bindingConfiguration="webHttpBinding"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="2147000000" />
</webHttpEndpoint>
</standardEndpoints>
<bindings>

<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" name="ImagesBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
<binding name="webHttpBinding" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="10485760" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="1000000" />
</binding>
</webHttpBinding>

当我中断 encodedString 片段并且它与我的预期不匹配时,问题就很明显了。如果我将整个字符串复制到另一个从 base64 字符串生成图像的在线图像中,则它不是有效图像。在这一点上,我被卡住了,无法确定为什么我无法从 dropzone 中读取。

最佳答案

如果我解码你收到的 base64 字符串 LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5T1RUV0I1RFZZdTVlM2NTNQ0KQ29udG

我明白了

------WebKitFormBoundaryOTTWB5DVYu5e3cS5

所以我猜你在引用这篇文章中的元素时遇到了问题: Uploaded file only contains "WebKitFormBoundary"

关于javascript - Dropzone JS 上传到 WCF 获取错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38694753/

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