我試圖從jQuery表單文章上傳文件。我用了jQuery代碼沿以下HTML:如何使用WCF服務和javascript表單文件上傳.doc文件
<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();
在我用下面的服務方法
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();
}
這工作的WCF的一部分,當我嘗試上傳一個.txt
文件。但是,我需要上傳帶有不同表格和格式的.doc
文件。
當我試圖這樣做時,AllTxtFiles.doc
包含一些不可理解的文本。
我試了一整天,但失敗(可能是因爲我是WCF新手)。任何人都可以請幫我這麼做嗎?
你能上傳大文本文件? –