HttpPostedFileBase在生产中为null,但不是测试环境

时间:2014-06-06 02:59:52

标签: c# asp.net

我有2个环境,直到今天我认为完全一样。 (沙箱和生产)

我有一个移动设备,将文件发布到服务器。这在沙盒环境中工作正常,但是当我在生产中运行相同的东西时,该文件为null。我检查了web.config和<httpRuntime requestValidationMode="2.0" maxRequestLength="264768888" /> 在两种环境中都设置为以上。我在这里错过了什么?有什么不同?这可能是IIS中的一些设置吗?代码如下:

我测试的文件是64k。

服务器代码

[HttpPost, Url("v3/organizations/{organizationId?}/SyncVideo/")]
public virtual void SyncVideo(HttpPostedFileBase file, Guid? organizationId) {
    if (file == null)
        throw new HttpBadRequestException("file is missing");

    if (organizationId.IsNull()) throw new HttpNotFoundExecption();
    if (organizationId != RESTContext.OrganizationId) throw new HttpNotAuthorizedException();

    var basePath = RESTContext.Config.VideoPath;

    //using (new Impersonator(RESTContext.Config.VideoPathUsername, RESTContext.Config.VideoPathDomain,
    //    RESTContext.Config.VideoPathPassword)) {
    //    if (!Directory.Exists(basePath))
    //        Directory.CreateDirectory(basePath);
    //    file.SaveAs(basePath + @"\" + file.FileName);
    //}
}

客户代码

public void UploadFile(string url, string path) {
    Stream stream = null;
    HttpClient client = null;
    MultipartFormDataContent content = null;
    StreamContent streamContent = null;

    try {
        stream = Platform.FileSystem.ReadStreamFromFile(path);
        Platform.Console.WriteLine(stream.Length.ToString());
        client = new HttpClient();
        content = new MultipartFormDataContent();

        foreach (var header in headers)
            client.DefaultRequestHeaders.Add(header.Key, header.Value);

        streamContent = new StreamContent(stream);

        content.Add(streamContent, "file", Path.GetFileName(path));
        var message = client.PostAsync(url, content).Result;

        if (message.StatusCode != HttpStatusCode.OK) {
            Platform.Console.WriteLine(message.ToString());
            throw new Exception(message.ToString());
        }
    } finally {
        content.NullSafeDispose();
        client.NullSafeDispose();
        stream.NullSafeDispose();
        streamContent.NullSafeDispose();
    }
}

1 个答案:

答案 0 :(得分:2)

在服务器上安装.net framework 4.5.1修复它。之前只有4.5。我确定为什么会这样,但它确实......