0
我目前正在使用C#和WCF的Web服務。我需要做一個上傳方法,所以我遵循this tutorial。現在我可以通過表單上傳文件和文件上傳的文件夾中,但在瀏覽器中它總是返回我有一個錯誤(像在阿賈克斯指定)這裏的所有代碼:WCF/C#上傳文件
IWebService.cs
[ServiceContract]
public interface IWebService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
void UploadFile(string fileName, Stream stream);
}
WebService.svc
public void UploadFile(string fileName, Stream stream)
{
string FilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Uploads"), fileName);
int length = 0;
using (FileStream writer = new FileStream(FilePath, FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
}
TestClient.html
<head>
<script type="text/javascript">
function UploadFile() {
fileData = document.getElementById("fileUpload").files[0];
var data = new FormData();
$.ajax({
url: 'http://localhost:1945/Service1.svc/UploadFile?fileName=' + fileData.name,
type: 'POST',
data: fileData,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: "application/octet-stream", // Set content type to false as jQuery will tell the server its a query string request
success: function (data) {
alert('successful..');
},
error: function (data) {
alert('Some error Occurred!');
}
});
}
</script>
<title></title>
</head>
<body>
<div>
<div>
<input type="file" id="fileUpload" value="" />
<br />
<br />
<button id="btnUpload" onclick="UploadFile()">
Upload
</button>
</div>
</div>
</body>
感謝您的評論它幫助我很多。您是否知道我需要爲錯誤添加回報? – PokeRwOw