【问题标题】:How to pass multiple images in WCF REST service using postman如何使用邮递员在 WCF REST 服务中传递多个图像
【发布时间】:2016-11-21 01:19:55
【问题描述】:

在这里我一次只能选择一张图片。但我想选择多张图片。请有人帮帮我。如果我选择多张图片,它也只保存一张图片。我正在使用邮递员服务选择图片。

public ServiceResponse<string> UploadDoc(Stream fileContents)
            {

                IncomingWebRequestContext woc = WebOperationContext.Current.IncomingRequest;


                string fileName = woc.Headers["fileName"];
                if (fileName.Length <= 0)
                {
                    throw new Exception("File not attached");
                }


                if (fileName == "")
                {
                    throw new Exception("Please send fileName in header");
                }

                //string userId = woc.Headers["UserAuthor"];

                string fileNameUnique = Guid.NewGuid() + fileName;
                string upload_FilePath = @"" + docFolder + "\\" + fileNameUnique;
                string fileUrl = hostUrl + "/Document/" + fileNameUnique;
                try
                {

                    int length = 0;

                    using (FileStream writer = new FileStream(upload_FilePath, FileMode.Create))
                    {
                        int readCount;
                        var buffer = new byte[8192];

                        while ((readCount = fileContents.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            writer.Write(buffer, 0, readCount);
                            length += readCount;
                        }
                    }

                    var response = new ServiceResponse<string>
                    {
                        ResponseObject = fileUrl
                    };

                    return response;

                }
                catch (Exception e)
                {
                    var response = new ServiceResponse<string>
                    {
                        IsError = true,
                        ExceptionObject = new ExceptionModel()
                        {
                            ErrorMessage = e.Message,
                            Source = e.Source,
                            Severity = 0,
                            KeyParameter = new[] { "ServiceError" }
                        }
                    };
                    return response;
                }

            }

【问题讨论】:

  • 您的服务操作的名称是public ServiceResponse UploadDoc(Stream fileContents)。这似乎只允许一个文档。
  • 我也不能保存单个对象,如果我保存单个对象,我会得到错误的图像。你能说我怎么做吗?
  • 虽然我们很强大,但 StackOverflow 还没有进化出读心术的能力。你的问题无法回答。如果您遇到错误,那么您必须告诉我们错误是什么。否则,我们应该如何提供帮助? stackoverflow.com/help/how-to-ask

标签: wcf wcf-rest


【解决方案1】:

如果我理解您的问题,您可以尝试将您的 Stream 参数更改为这样的类。

public class FilesRequest
{
     public ICollection<Stream> MyStreams { get; set; }
}

public ServiceResponse<string> UploadDoc(FilesRequest request)
{
   // Process the collection in your input, request.MyStreams
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2014-03-04
    • 2018-11-01
    • 2018-11-05
    • 2012-03-12
    • 2019-04-08
    • 2012-04-17
    相关资源
    最近更新 更多