我已经编写了一个ajax函数和WCF类,用于将文件上传到服务器中的文件夹,但是不幸的是,图像已上传到该文件夹,但无法打开。当我尝试打开它们时,出现错误,提示其损坏,当我上传.doc,pdf或.excel文件时也会发生同样的事情。
但仅适用于.txt文件。
这是我的WCF接口类
[ServiceContract]
public interface IFileUpload
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Upload/{fileName}")]
String UploadFile(String fileName, Stream data);
课程的实现
public class FileUpload : IFileUpload
{
public String UploadFile(String fileName, Stream streamData)
{
String fileId = fileName;
Logger.Info("file Name" + fileName);
string FilePath = Path.Combine(HostingEnvironment.MapPath("~/Uploads"), fileName);
Logger.Info("file path" + FilePath);
int length = 0;
using (FileStream writer = new FileStream(FilePath,
FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = streamData.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
streamData.Close();
}
return "fileId";
}
用于从UI提交文件的Ajax函数
$.ajax({
url: fileUpload + $scope.filename,
method: "POST",
cache: false,
data: data,
contentType: "application/octet-stream",
enctype: 'multipart/form-data',
processData: false
}).success(function (response) {
document.getElementById('file2').value = "";
console.log("File uploaded successfully");
}).error(function (xhr, rrr, error) {
console.log("Error while uploading the file: - " + error);
})
Web.config脚本绑定配置
<bindings>
<webHttpBinding>
<binding name="webBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
<binding name="FileWorker.webHttpBinding" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647" transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
<binding name="webBindingHTTPS" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
<bindings>
我到处都进行了研究,但似乎没有任何作用