1

在创建 WCF REST 服务时,我在 json 中接收数据并能够保存在数据库中。现在我需要提供使用相同服务上传文件(可选、图像或视频)的选项。我尝试发送字节数组,但它给出了错误的请求错误,可能是因为序列化了这么长的数组。我读到要上传我需要使用流的大文件。在发送其他参数时我将如何做到这一点?我正在创建此服务以从移动设备接收数据。这是我的服务接口

[WebInvoke(UriTemplate = "SaveFBPost", 
    Method = "POST", 
    BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
void SaveFacebookPost(FBPostData fbPostData);

公共类 FBPostData:

[DataMember]
public string scheduleDate { get; set; }

[DataMember]
public string userId { get; set; }

[DataMember]
public string groupId { get; set; } 

[DataMember]
public string postText { get; set; }

[DataMember]
public byte[] file { get; set; }

[DataMember]
public string fileType { get; set; }

[DataMember]
public string accessToken { get; set; }
4

2 回答 2

1

我不得不使用来自 android 的分段上传和分段解析器的流来解决这个问题。我使用 Apache mime 库来上传文件并发送如下参数:

HttpPost postRequest = new HttpPost(
                context.getString(R.string.url_service_fbpost));
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        if(postData.data != null && !"".equals(postData.fileName)){
            ByteArrayBody bab = new ByteArrayBody(postData.data, postData.fileName);
            reqEntity.addPart("uploaded", bab);
        }
        reqEntity.addPart("scheduleDate", new StringBody(postData.scheduleDate));
        reqEntity.addPart("userId", new StringBody(postData.userId));
        reqEntity.addPart("groupIds",
                new StringBody(postData.groupIds.toString()));
        reqEntity.addPart("postText", new StringBody(postData.postText));
        reqEntity.addPart("postType", new StringBody(postData.postType));
        reqEntity.addPart("accessToken", new StringBody(postData.accessToken));
        if(postData.postId != null && postData.postId.length() > 0) {
            reqEntity.addPart("postId", new StringBody(postData.postId));
        }
        postRequest.setEntity(reqEntity);

之后我使用 c# 多部分解析器来获取文件和参数。这是服务代码:

[OperationContract]
[WebInvoke(Method = "POST",
        UriTemplate = "UploadFBPost",
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void UploadFBPost(Stream stream);

 public void UploadFBPost(Stream stream)
{
    MultipartParser parser = new MultipartParser(stream);

    // Saves post data in database
    if (parser.Success)
    {
        string fileName = null, userId = null, postText = null, postType = null, accessToken = null;
        DateTime scheduleDate = DateTime.Now;
        string[] groupIds = null;
        int postId = 0;

        // Other contents
        foreach (MyContent content in parser.MyContents)
        {
            switch (content.PropertyName)
            {
                case "scheduleDate":
                    if (string.IsNullOrEmpty(content.StringData.Trim()))
                        scheduleDate = DateTime.Now;
                    else
                        scheduleDate = DateTime.ParseExact(content.StringData.Trim(), "M-d-yyyy H:m:s", CultureInfo.InvariantCulture);
                    break;

                case "fileName":
                    fileName = content.StringData.Trim();
                    break;

                case "userId":
                    userId = content.StringData.Trim();
                    break;

                case "postText":
                    postText = content.StringData.Trim();
                    break;

                case "accessToken":
                    accessToken = content.StringData.Trim();
                    break;

                case "groupIds":
                    groupIds = content.StringData.Trim().Split(new char[] { ',' });
                    break;

                case "postType":
                    postType = content.StringData.Trim();
                    break;

                case "postId":
                    postId = Convert.ToInt32(content.StringData.Trim());
                    break;
            }
        }

        string videoFile = null, imageFile = null;
        if (parser.FileContents != null)
        {
            string filePath = GetUniqueUploadFileName(parser.Filename);
            File.WriteAllBytes(filePath, parser.FileContents);
            if (postType == "photo")
                imageFile = Path.GetFileName(filePath); 
            else 
                videoFile = Path.GetFileName(filePath); 
        }
    }
}

您确实需要根据您发送的数据修改多部分解析器。希望这将节省一些时间。

感谢雷的帮助。

还有一件事。我不得不在 web config 中添加这些行:

<httpRuntime maxRequestLength="2000000"/>

<bindings>
  <webHttpBinding>
    <binding maxBufferSize="65536"
             maxReceivedMessageSize="2000000000"
             transferMode="Streamed">
    </binding>
  </webHttpBinding>
</bindings>
于 2013-05-20T09:58:17.113 回答
1

我将从增加 web.config 中特定绑定的 maxBufferSize 和 maxArrayLength 开始,看看是否能解决问题。

您还应该能够获取有关该错误的更多细节,这样您就可以确切地看到为什么会出现 400 错误。

这篇博客文章过去对我也很有用——底部也有一些很好的链接。

还要看一下Stream类。不确定“发送其他参数”是什么意思 - 如果您能澄清这一点,我们可以为您提供更多指导。

于 2013-05-09T20:35:59.697 回答