1

I tried to upload a file from a jQuery form post. I used the following html along with the jQuery codes:

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="">
     <input type="file" name="uploadcv" id="uploadcv" size="30" />
</form>

Javascript:

var url = ServiceLocation + "/UploadFile";   //ServiceLocation = my service location

$("#file_upload_form").attr("action", url);

$("#file_upload_form").submit();

In the WCF part I used the following service method

public string UploadFile(Stream  inputStream)
{
        const int bufferSize = 8 * 1024 * 2;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = inputStream.Read(buffer, 0, bufferSize);
        Stream outputStream = null;
        string newFileName = @"D:\AllTxtFiles.doc";
        outputStream = new FileInfo(newFileName).OpenWrite();

        while (bytesRead > 0)
        {
            outputStream.Write(buffer, 0, bufferSize);
            bytesRead = inputStream.Read(buffer, 0, bufferSize);
        }
        inputStream.Close();
        outputStream.Close();

}

This works, when I try to upload a .txt file. However, I need to upload a .doc file with different tables and formatting.

When I tried to do that, AllTxtFiles.doc contains some non-understandable texts.

I tried and searched for the whole day, but failed (probably because I am new in WCF). Can anyone please help me to do that?

1
  • are you able to upload big text file ? Commented Sep 13, 2012 at 11:14

2 Answers 2

1

The form cannot be posted to the stream argument, only the stream itself:

function upload() {
  var request = new XMLHttpRequest();
  request.open('POST', 'wcf/WCFUploader.svc/Upload/');
  request.send(filePicker.files[0]);
} 

All the details are available here:

http://www.codeproject.com/Tips/757991/File-Upload-using-WCF-REST-API-and-JavaScript

Sign up to request clarification or add additional context in comments.

Comments

-1

Here are helpful links:

file upload to wcf using jquery

Uploading a file to WCF service using HTML5 and/or jQuery

http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSON

http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.