How to upload the image from the stream in WCF rest service

巧了我就是萌 提交于 2019-12-19 12:00:07

问题


I'm trying to create the wcf service which will upload the files like pdf,doc,xls,images but pdf, txt files are uploading and opening properly but when i'm trying to upload the image file then the file is getting uploaded but the image is not visible

  [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();
            }

回答1:


try byte array instead of stream and wrapping the class like this:

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

then simply write the file on disk like this:

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



回答2:


@mohammad check the below image how i'm trying to upload the image file how i'm trying to upload the image file

Thanks



来源:https://stackoverflow.com/questions/41849403/how-to-upload-the-image-from-the-stream-in-wcf-rest-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!