简体   繁体   中英

How to upload a .doc file using WCF service and javascript form post

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM