文件上传MVC移动浏览器

时间:2018-11-05 14:32:59

标签: c# .net asp.net-mvc

我正在尝试使用正常的HttpPostedFileBase在.net mvc应用程序中上传文件,从计算机浏览器尝试时一切正常,但是从iPhone浏览器尝试时,我将HttpPostedFileBase参数设置为null

我的表单的enctype属性设置为'multipart / form-data'

任何人都可以帮忙吗?

我的动作:,

public async Task<ActionResult> Index(HttpPostedFileBase file, string saveFolder)
    {
        try
        {
            if (file == null) return Json(new { Result = false, Message = "File is not nullable." });

            var httpClient = new HttpClient();

            var fileInfo = new FileInfo(file.FileName);
            var fileByte = new byte[] { };
            using (var ms = new MemoryStream())
            {
                file.InputStream.CopyTo(ms);
                fileByte = ms.ToArray();
            }


            var model = new
            {
                FileExtension = fileInfo.Extension.Replace(".", ""),
                FileName = file.FileName,
                FileType = 0,
                SaveFolder = saveFolder,
                File = Convert.ToBase64String(fileByte)
            };
            httpClient.DefaultRequestHeaders.Add("UserName", ConfigurationManager.AppSettings["VaultSecurityProWebApiUserName"]);
            httpClient.DefaultRequestHeaders.Add("Password", ConfigurationManager.AppSettings["VaultSecurityProWebApiPassword"]);
            //httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");

            var form = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");

            var response = await httpClient.PostAsync(ConfigurationManager.AppSettings["VaultSecurityProWebApiBaseUrl"] + "/api/File", form);


            response.EnsureSuccessStatusCode();
            httpClient.Dispose();
            string sd = response.Content.ReadAsStringAsync().Result;
            var returnModel = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultModel<FileResponseModel>>(sd);
            if (returnModel == null ||
                !returnModel.ResultStatus ||
                returnModel.Result == null ||
                !returnModel.Result.Any())
                return Json(new { Result = false, Message = returnModel?.Message });

            return Json(new { Result = true, Article = returnModel.Result.FirstOrDefault().FileDirectory });
        }
        catch (Exception ex)
        {

            throw;
        }
    }

我正在动态创建表单

 var formGuid = 'hiddenform' + (Math.floor(Math.random() * 10000));
        var form = $(document.createElement('form'))
            .attr('action', isVideo == true ? '/File/UploadVideo' : '/File/Upload')
            .attr('enctype', 'multipart/form-data')
            .attr('method', 'post')
            .attr('id', formGuid);


//element is the html file input element 
var fileInput = $element.clone();
        $(fileInput).attr('name', 'file');
        $(form).append(fileInput);

  $(form).submit();

0 个答案:

没有答案