【问题标题】:MVC + WCF + EF - File stream post - The remote server returned an error: (413) Request Entity Too LargeMVC + WCF + EF - 文件流发布 - 远程服务器返回错误:(413)请求实体太大
【发布时间】:2014-05-13 10:11:36
【问题描述】:

在我的 MVC 应用程序中,我使用 dropzone 控件来允许用户将文件从本地系统上传到 SQL 数据库。

if (Request.Files.Count > 0)
            {
                var name = Request.Files[0].FileName;
                var size = Request.Files[0].ContentLength;
                var type = Request.Files[0].ContentType;
var fileStream = Request.Files[0].InputStream;
 byte[] documentBytes = new byte[fileStream.Length];
                fileStream.Read(documentBytes, 0, documentBytes.Length);

                Documents databaseDocument = new Documents
                {
                    FileContent = documentBytes,
                    DocumentName = System.IO.Path.GetFileName(name),
                    DocumentSize = size,
                    DocumentType = type
                };                               
                bool result = this.updateService.SaveDocument(databaseDocument);
            }

“updateService”实际上是对 WCF 服务的引用。 我在上述代码中的“SaveDocument”调用中收到错误。 我已经按照其他论坛的建议设置了 uploadReadAheadSize(在 applicationHost.config 中)和 maxReceivedMessageSize(在 WCF 和 Web 配置文件中)。 这个错误仍然没有为我解决。

这会给出错误提示“远程服务器返回错误:(413) 请求实体太大”

【问题讨论】:

标签: asp.net-mvc entity-framework wcf asp.net-mvc-4


【解决方案1】:

如果您不想在传输太大的对象时遇到问题,可以使用流作为服务操作的参数。

服务接口:

[ServiceContract]
public interface IStreamedService
{
    [OperationContract]
    void PrepareUpload(long fileLength, string fileName);
    [OperationContract]
    void UploadFile(Stream fileStream);
}

服务实现:

public class StreamedService : IStreamedService
{
    private static long lengthOfFile;
    private static string nameOfFile;

    public void PrepareUpload(long fileLength, string fileName)
    {
        lengthOfFile = fileLength;
        nameOfFile = fileName;
    }

    public void UploadFile(Stream fileStream)
    {
        if(lengthOfFile==0 || string.IsNullOrEmpty(nameOfFile)) 
            throw new ArgumentException("Upload must be prepared");
        var bytes = new byte[lengthOfFile];
        var numberOfBytesToRead = bytes.Length;
        var numberOfReadBytes = 0;

        while (numberOfBytesToRead > 0)
        {
            var n = fileStream.Read(bytes, numberOfReadBytes, numberOfBytesToRead);
            if (n == 0)
                break;
            numberOfReadBytes += n;
            numberOfBytesToRead -= n;
        }

        var fsOut = new FileStream(string.Format(@"c:\temp\{0}", nameOfFile), FileMode.Create);
        fsOut.Write(bytes, 0, numberOfReadBytes);
        fsOut.Close();
    }
}

服务配置:

system.serviceModel>
<services>
  <service name="StreamedService.StreamedService">
    <endpoint address="net.tcp://localhost:60000/StreamedService"
      binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="Contracts.IStreamedService" />
  </service>
</services>
<bindings>
  <netTcpBinding>
    <binding name="NewBinding0" transferMode="Streamed" maxReceivedMessageSize="67108864" />
  </netTcpBinding>
</bindings>

客户端实现:

    var proxy = new ChannelFactory<IStreamedService>("MyEndpoint").CreateChannel();
    var fs = new FileStream(@"c:\temp\FileToUpload.zip", FileMode.Open);
    proxy.PrepareUpload(fs.Length, "uploadedFile.zip");
    proxy.UploadFile(fs);

客户端配置:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NewBinding0" transferMode="Streamed" maxReceivedMessageSize="67108864" />
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://localhost:60000/StreamedService"
            binding="netTcpBinding" bindingConfiguration="NewBinding0"
            contract="Contracts.IStreamedService" name="MyEndpoint">
        </endpoint>
    </client>
</system.serviceModel>

上面的内容也适用于 basicHttpBinding。当然,您可以在服务器端使用 MemoryStream 而不是 FileStream,然后将其反序列化为您想要保存到 DB 的某个实体。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2015-03-21
    • 2021-10-08
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多