Wednesday, January 25, 2017

Upload File In WCF REST Api Using Stream Data


I got the solution to upload the files but the problem was with the image files when i try to upload the doc,xls,ppt,txt etc files they were uploading and opening properly but when i try to upload any image file the file was getting uploaded but not opening. Then i did a lot of googling , search tech forums then finally i got the one more solution for that problem now you can upload files like docx,ppt,xls,txt,jpeg,png etc.
 
In  interface


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

  
 In class

 
MultipartParser parser = new MultipartParser(streamData);

                if (parser.Success)
                {
                    string fileName = parser.Filename;
                    string contentType = parser.ContentType;
                    byte[] fileContent = parser.FileContents;
                    FileStream fileToupload = new FileStream("Path Name", FileMode.Create);
                    fileToupload.Write(fileContent, 0, fileContent.Length);
                    fileToupload.Close();
                    fileToupload.Dispose();
                    streamData.Close();
                }

For using the multipartparser class file you need to download it from the below link


In Web.Config file

<bindings>
      <webHttpBinding>
        <binding name="WebConfiguration"
                 maxReceivedMessageSize="2147483647"
                 maxBufferSize="2147483647"
                 closeTimeout="00:01:00" openTimeout="00:01:00"
                 receiveTimeout="00:10:00" sendTimeout="00:01:00"
                 transferMode="Streamed">
         
        </binding>
      </webHttpBinding>
    </bindings>



 

No comments:

Post a Comment

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...