Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从angular发送文件以创建新文件并从.net下载_C#_Asp.net_.net_Angular_Typescript - Fatal编程技术网

C# 从angular发送文件以创建新文件并从.net下载

C# 从angular发送文件以创建新文件并从.net下载,c#,asp.net,.net,angular,typescript,C#,Asp.net,.net,Angular,Typescript,我一直在尝试下载.xml文件,但遗憾的是没有成功。 从角度看,我正在发送*.xml文件。在.NET端,我将它处理并创建一个新的*.xml文件。我需要下载这个新文件,但是我真的不知道如何解决它 这是我的文件组件。ts: OnSubmit(value, File) { const params1: FormData = new FormData(); params1.append('File', this.fileToUpload, this.fileToUpload.name); pa

我一直在尝试下载.xml文件,但遗憾的是没有成功。 从角度看,我正在发送
*.xml
文件。在.NET端,我将它处理并创建一个新的
*.xml
文件。我需要下载这个新文件,但是我真的不知道如何解决它

这是我的
文件组件。ts

OnSubmit(value, File) {
  const params1: FormData = new FormData();
  params1.append('File', this.fileToUpload, this.fileToUpload.name);
  params1.append('ProcessingMode', value.processingMode);
  params1.append('StartDate', value.startDate.formatted);

  const params = {
      'File': this.fileToUpload,
      'ProcessingMode': value.processingMode,
      'StartDate': value.startDate.formatted
  };

        this.mapsConfigurationService.postFile(value, this.fileToUpload, value.startDate.formatted)
        .subscribe((res: any) => {
                  this.downloadFile(res, 'xml'); debugger;
                  this.xmlProcessing = false;
              },
                  (err) => {
                      if (err.status === 401) {
                          //  this.router.navigate(['unauthorized']);
                      } else {
                          this.xmlProcessing = false;
                      }
                  });




downloadFile(data, type) {
  const fileName = 'test';
  var contentType;
  if (type === 'xml') {
      contentType = 'text/xml';
  }
  var blob = new Blob([data._body], { type: contentType });
  const dwldLink = document.createElement('a');
  const url = URL.createObjectURL(blob);
  const isSafariBrowser = navigator.userAgent.indexOf('Safari') !== -1 && navigator.userAgent.indexOf('Chrome') === -1;
  if (isSafariBrowser) {
      dwldLink.setAttribute('target', '_blank');
  }
  const fullFileName = fileName + '.' + type;
  dwldLink.setAttribute('href', url);
  dwldLink.setAttribute('download', fullFileName);
  dwldLink.style.visibility = 'hidden';
  document.body.appendChild(dwldLink);
  dwldLink.click();
  document.body.removeChild(dwldLink);}
这是
service.ts

postFile(value: any, fileToUpload: File, startDate) {
const formData: FormData = new FormData();
formData.append('File', fileToUpload, fileToUpload.name);
formData.append('ProcessingMode', value.processingMode);
formData.append('StartDate', '2015-05-23');
return this.http
  .post(this.Url, formData);
}

这是后端:

[HttpPost, DisableRequestSizeLimit]
    public ActionResult UploadFile()
    {
        try
        {
            var xml = Request.Form.Files["File"].ToString();
            var httpRequest = HttpContext.Request.Form;
            var postedFile = httpRequest.Files["File"];
            string outputFile = Request.Form["info"].ToString();
            var startDate = Request.Form["StartDate"];
            var file = httpRequest.Files[0];
            string fullPath = "";
            string folderName = "Upload";

            string antFile = @"C:\ant.bat";
            string build = @"C:\build.xml";


            string rootPath = @"C:\Users";
            string newPath = Path.Combine(rootPath, folderName);
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                fullPath = Path.Combine(newPath, fileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
            }

            return PhysicalFile(@"C:\Book1.xml", "application/xml", "Book1.xml");
        }
        catch (System.Exception ex)
        {
            return StatusCode(500);
        }
    }

我收到错误500,我知道问题出在RequestHeaders上,但我不知道如何解决它,也不知道下载任何文件时从哪一方下载。。。我正在后台使用此代码 并通过正常http请求从angular生成和请求代码

            var myFile :: your file

    if (System.IO.File.Exists (myFile.Path)) {// to know if the file is Exist or not
     //Process File Here ...
    } else {
      return Json ("NotFound");
    }
    string contentType = "application/xml"; 
    HttpContext.Response.ContentType = contentType;

    var result = new FileContentResult (System.IO.File.ReadAllBytes (myFile.Path), contentType) {

      FileDownloadName = $"{myFile.Title }" // + myFile.Extension

    };

   // System.IO.File.Delete (myFile.Path);  //if you want to delete the file after download

    return result;

您可能希望向该catch块添加一些日志记录,然后检查它执行的异常是什么
returnphysicalfile(@“C:\Book1.xml”、“application/xml”、“Book1.xml”)并退出,它不会捕获任何异常,但在开发人员工具中,我收到500个内部服务器错误。在该错误下方有一个catch块,执行
catch(System.Exception ex)
,然后
返回StatusCode(500)如下。我想这里捕获了异常,这就是为什么您会收到500个错误的原因