如何将上载的.CSV文件发送到Web服务

时间:2019-03-30 09:41:34

标签: .net angular typescript asp.net-web-api2

我试图弄清楚如何发送从UI(角度)上载的文件并将其发送到.NET Web服务,以便它解析CSV文件并创建对象列表。

我目前对逻辑应该如何工作的想法是

文件上传---> Web服务(用于解析文件)-> Web API --->数据库

对吗?

将该文件发送到服务然后从服务发送到API控制器时,我缺少什么?

HTML:

  <input type="file" (change)="onFileSelected($event)">
                <button type="submit" (click)="onUpload"   class="button">Upload</button>

Web服务:

 static void Main(string[] args)
        {

            string currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory = new DirectoryInfo(currentDirectory);
            var fileName = Path.Combine(directory.FullName, "sample-data.csv");
            var fileContents = ReadMonitoredEvent(fileName);
        }

        public static string ReadFile(string fileName)
        {
            using (var reader = new StreamReader(fileName))
            {
                return reader.ReadToEnd();
            }
        }


        public static List<MonitoredEvent> ReadMonitoredEvent(string fileName)
        {
            var monitoredEventResults = new List<MonitoredEvent>();
            using (var reader = new StreamReader(fileName))
            {
                string line = "";
                reader.ReadLine();
                while ((line = reader.ReadLine()) != null)
                {
                    var monitoredEvent = new MonitoredEvent();
                    string[] values = line.Split(',');              
                    DateTime eventDate;
                    TimeSpan eventTime;
                    float tlc;
                    float totalOrder;

                    //String.Format("{Short Date}", DateTime.Now);
                    if (DateTime.TryParse(values[3], out eventDate))
                    {
                        var date = eventDate.Date;
                        monitoredEvent.EventDate = date;
                    }
                    if(TimeSpan.TryParse(values[4], out eventTime))
                    {
                        monitoredEvent.Time = eventTime;
                    }
                    if (float.TryParse(values[6], out tlc))
                    {
                        monitoredEvent.TLC = tlc;
                    }
                    if (float.TryParse(values[5], out totalOrder))
                    {
                        monitoredEvent.TotalOrder = totalOrder;
                    }
                    monitoredEvent.Venue = values[0];
                    monitoredEvent.Event = values[1];
                    monitoredEvent.Section = values[2];
                    monitoredEvent.TicketType = values[7];
                    monitoredEvent.Source = values[8];
                    monitoredEvent.TicketPage = values[9];

                    monitoredEventResults.Add(monitoredEvent);

                }
            }
            return monitoredEventResults;


        }

我没有控制器的代码,也没有打字稿。

我希望该文件能够从用户上载到存储在数据库中的所有对象中。

1 个答案:

答案 0 :(得分:0)

您的逻辑几乎是正确的,除了我们不是在角度使用Http Client的Web服务之外,我建议您阅读教程,例如this

更新:

这是将文件上传到Web api的功能,其C#代码为:

[HttpPost]
[Route("File/Post")]
public IHttpActionResult UploadFile()
{
   var httpRequest = HttpContext.Current.Request;
   var postedFile = httpRequest.Files["File"];
   //the parsing logic
   return Ok();
}

而角部分是:

postFile(fileToUpload: File) {
   const formData: FormData = new FormData();
   const tmpUrl=myUrl+"File/Post";
   formData.append("File", fileToUpload, fileToUpload.name);
   return this.http.post(tmpUrl, formData).pipe();
}

我简化了它,但这足以使您前进