我尝试使用WCF上传pdf /文件。
我的问题是,我上传的文件尚未完成
有人可以帮助我这是我的功能上传:
public string UploadFile(FileUploadMessage request)
{
Stream fileStream = null;
Stream outputStream = null;
try
{
fileStream = request.FileByteStream;
string rootPath = @"C:\WCF";
DirectoryInfo dirInfo = new DirectoryInfo(rootPath);
if (!dirInfo.Exists)
{
dirInfo.Create();
}
string newFileName = Path.Combine(rootPath, Guid.NewGuid() + ".pdf");
outputStream = new FileInfo(newFileName).OpenWrite();
const int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead = fileStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.Write(buffer, 0, bufferSize);
bytesRead = fileStream.Read(buffer, 0, bufferSize);
}
return newFileName;
}
catch (IOException ex)
{
throw new FaultException<IOException>(ex, new FaultReason(ex.Message));
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
if (outputStream != null)
{
outputStream.Close();
}
}
}
这是我的配置
<binding name="BasicHttpBinding_ITransferService" closeTimeout="04:01:00"
openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00"
allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
messageEncoding="Mtom" textEncoding="utf-8"
transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="128"
maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
答案 0 :(得分:0)
如果要将byte [](例如文件)流式传输到WCF操作,则操作只需要一个Stream类型的参数。问题是你需要知道接收端(服务)上字节[]的长度。
一种解决方案是对两个单独的服务操作进行两次调用
public void SetLength(long length)
public void Upload(Stream stream)
保存第二次通话中使用的第一次通话的长度。
使用此解决方案,您无需关心将配置中的MaxRecievedMessageSize设置为某个数字。
答案 1 :(得分:0)
它应该是这样的(检查这个例子http://www.dotnetfunda.com/articles/show/2008/upload-a-file-using-aspnet-file-upload-control-and-wcf):
class FileUploadService : IFileUploadService
{ public bool UploadFileData(FileData fileData) { bool result = false; 尝试 { //设置要保存文件的位置 string FilePath = Path.Combine({来自某些设置的路径},fileData.FileName);
//If fileposition sent as 0 then create an empty file
if (fileData.FilePosition == 0)
{
File.Create(FilePath).Close();
}
//Open the created file to write the buffer data starting at the given file position
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
}
}
catch (Exception ex)
{
//throw FaultException<>();
}
return result;
}
}