【问题标题】:How to upload the image from the stream in WCF rest service如何从 WCF 休息服务中的流上传图像
【发布时间】:2017-06-10 12:06:03
【问题描述】:

我正在尝试创建 wcf 服务,该服务将上传 pdf、doc、xls、图像等文件,但 pdf、txt 文件正在上传和正确打开,但是当我尝试上传图像文件时,文件是正在上传,但图片不可见

  [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "Upload/{fileName}")]
        string Upload(string fileName, Stream fileContents);


using (FileStream fs = new FileStream("my path", FileMode.Create))
            {
                fileContents.CopyTo(fs);
                fileContents.Close();
            }

【问题讨论】:

    标签: web-services rest api wcf image-upload


    【解决方案1】:

    @mohammad 检查下图我是如何尝试上传图像文件的 how i'm trying to upload the image file

    谢谢

    【讨论】:

    • @mohammad 我得到了解决方案,但我的帐户被禁止了感谢您的帮助,我将把解决方案粘贴到我的博客上saishborkar.blogspot.in
    【解决方案2】:

    尝试字节数组而不是流并像这样包装类:

    public class Input
    {
        [DataMember]
        public string fileName { get; set; }
        [DataMember]
        public byte[] fileContents { get; set; }
    }
    

    然后像这样简单地将文件写入磁盘:

    public string Upload(Input input)
    {
        File.WriteAllBytes(input.fileName, input.fileContents);
        return "OK";
    }
    

    【讨论】:

    • 你能解释一下如何使用上面的例子谢谢
    • 感谢您的回复。我使用这种方式是否正确,因为我是 wcf 服务的新手 [WebInvoke(Method = "POST")] string Upload(Input input);跨度>
    • 我正在使用邮递员上传文件如何使用邮递员在上述代码中传递文件名
    • 亲爱的@SaishBorkar。你不能用邮递员上传这样的文件。请看看这个:stackoverflow.com/questions/9734941/…
    • 感谢您的解决方案,我从您的解决方案中得到了灵感
    猜你喜欢
    • 2012-10-11
    • 2013-11-05
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多