0

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

2 Answers2

0

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";
}
Mohammad
  • 2,724
  • 6
  • 29
  • 55
  • Can you please explain how to use this with the above example thanks – Saish Borkar Jan 27 '17 at 05:42
  • Thanks for your reply.i'm using like this way is it correct as i'm new to wcf service [WebInvoke(Method = "POST")] string Upload(Input input); – Saish Borkar Jan 27 '17 at 08:39
  • i'm using postman to upload the files how to pass the file name in the above code using postman – Saish Borkar Jan 27 '17 at 08:42
  • dear @SaishBorkar. you cant upload file like this with postman. please take a look at this: http://stackoverflow.com/questions/9734941/upload-file-from-html-form-multipart-form-data-to-wcf-rest-service-as-a-stream – Mohammad Jan 27 '17 at 09:51
  • Thanks for the solution i got the idea from your solution – Saish Borkar Jan 27 '17 at 11:30
0

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

Thanks

  • @mohammad I got the solution but my account got banned thanks for your help i will paste the solution on my blog http://saishborkar.blogspot.in/ – Saish Borkar Jan 27 '17 at 12:43