您可以尝试一些事情,您没有提及您使用的协议或托管它的方式,所以我假设它可能是 IIS7 和您使用的肥皂。在 Web 服务的 web.config 文件中,您可以添加以下内容,这将增加允许传输的文件大小而不会出现 404 错误:
<system.web>
<httpRuntime executionTimeout="999999" maxRequestLength="2097151" />
...
</system.web>
您可能想要对 Web 服务的 web.config 文件执行的第二件事以允许大文件传输:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2000000000" />
</requestFiltering>
</security>
</system.webServer>
另一种可能:
<location path="Copy.asmx"> <!-- Name of you asmx -->
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600"/> <!-- 100 megs -->
</requestFiltering>
</security>
</system.webServer>
</location>
通过 Web 服务发送 byte[] 的主要问题是它们被放入 SOAP 主体中,该主体被编码为 base 64 字符串。像这样对文件进行编码会使soap 正文中的文件大小增加三分之二(即,6 MB 的文件通过网络变成9 MB 的文件)。
另一种可能性是在传输之前将您的数据“分块”分成更小的部分,这可能是您所需要的。根据网络速度、服务器资源等因素,chunkSize(设置为 500KB)可能是提高上传性能的关键因素。
/// <summary>
/// Chunk the file and upload
/// </summary>
/// <param name="filename"></param>
private void UploadVideo(string filename)
{
#region Vars
const int chunkSize = 512000;
byte[] bytes = null;
int startIndex, endIndex, length, totalChunks;
WS.UploadRequest objRequest = new WS.UploadRequest();
#endregion
try
{
if (File.Exists(filename))
{
using (WS.UploadService objService = new WS.UploadService())
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//// Calculate total chunks to be sent to service
totalChunks = (int)Math.Ceiling((double)fs.Length / chunkSize);
//// Set up Upload request object
objRequest.FileName = filename;
objRequest.FileSize = fs.Length;
for (int i = 0; i < totalChunks; i++)
{
startIndex = i * chunkSize;
endIndex = (int)(startIndex + chunkSize > fs.Length ? fs.Length : startIndex + chunkSize);
length = endIndex - startIndex;
bytes = new byte[length];
//// Read bytes from file, and send upload request
fs.Read(bytes, 0, bytes.Length);
objRequest.FileBytes = bytes;
objService.UploadVideo(objRequest);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error- {0}", ex.Message));
}