Http post方法在使用c#获取响应之前挂起

时间:2016-07-11 14:20:23

标签: c# asp.net-mvc wcf

下面的代码工作正常,高达300mbfile上传,当iam上传超过512 mb时抛出错误,

当iam上传大文件时,我的代码在文件流式写入后挂起,

我需要上传超过2Gb的大文件

        var fileFolderObj1 = new FileFolder();
        var postURL = apiBaseUri + "/filefolder/create/file/user/" + userId; // +"?type=file";= HttpStatusCode.OK;
        HttpWebResponse response = null;

        if (logService != null)
        {
            var sb = new StringBuilder();
            sb.AppendLine("Parameters:");
            sb.AppendLine("CloudId: " + cloudId);
            sb.AppendLine("NewName: " + newName);
            sb.AppendLine("ParentId: " + parentId);
            sb.AppendLine("FileSize: " + fileSize);
            sb.AppendLine("FilePath: " + filePath);
            sb.AppendLine("URL: " + postURL);

        }

        try
        {
            string selectedFile = null;
            selectedFile = filePath;
            var fi = System.IO.Path.GetFileName(filePath);

            ////commented for some reason
            var postParameters = new Dictionary<string, object>();
            postParameters.Add("file", new FileParameter(filePath, ""));
            postParameters.Add("parentId", parentId);
            postParameters.Add("newName", fi);
            postParameters.Add("cloudId", cloudId);
            postParameters.Add("isSecure", isSecure);
            postParameters.Add("fileSize", fi.Length);
            var userAgent = "Desktop";
            var formDataBoundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");

            var uri = new Uri(postURL);
            var createFileRequest = WebRequest.Create(uri) as HttpWebRequest;
            this.SetBasicAuthHeader(createFileRequest, userId, password);
            createFileRequest.ContentType = "multipart/form-data";
            createFileRequest.Method = "PUT";
            createFileRequest.Timeout = System.Threading.Timeout.Infinite;
            createFileRequest.KeepAlive = true;
            createFileRequest.UserAgent = userAgent;
            createFileRequest.CookieContainer = new CookieContainer();
            try
            {
                using (var requestStream = createFileRequest.GetRequestStream())
                {
                    logService.Debug("GetRequestStream called", "CreateFile");
                    WriteMultipartFormData(postParameters, formDataBoundary, requestStream);

                    requestStream.Flush();
                    requestStream.Dispose();

                }
                using (response = (HttpWebResponse)createFileRequest.GetResponse())
                {

                    var reslut = new StreamReader(response.GetResponseStream()).ReadToEnd();

                    fileFolderObj1 = JsonConvert.DeserializeObject<FileFolder>(reslut);


                }
            }
            catch (Exception exc)
            {
                fileFolderObj1 = null;
                if (logService != null)
                {

                }

                if (response != null)
                {
                    code = response.StatusCode;
                }
            }

        }
        catch (Exception exc)
        {
            if (logService != null)
            {

            }
        }

        return fileFolderObj1;
    }

0 个答案:

没有答案