简体   繁体   中英

Upload Csv to solution explorer

I'm trying to build an application in which a user can upload a csv file and convert it into XML. Currently my controller can create .txt file in a temp folder. However when I open the txt file it comes out all corrupted as below: 上载

I have two questions 1. How can I make it so that the file displays properly ie as items separated by commas? 2. How can I change my code to make the file upload into my solution explorer

Here is the relevant controller code:

[HttpPost("UploadFiles")]
    public async Task<IActionResult> FileUpload(List<IFormFile> files)
    {
        long size = files.Sum(f => f.Length);
        var filePaths = new List<string>();
        foreach (var formFile in files)
        {
            if(formFile.Length > 0)
            {
                var filePath = Path.GetTempPath()+ Guid.NewGuid().ToString()+".txt";
                filePaths.Add(filePath);

                using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    await formFile.CopyToAsync(stream);
                }
            }
        }
        return Ok(new { count = files.Count, size, filePaths });
    }

Any suggestions would be much appreciated Thanks in advance

When the file is corrupt I think the conversion doesn't work. Try for now uploading it without any conversion.

For the second question you can do the following

var filePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid().ToString()}.csv"); // or whatever extension you are actually having without modifying the original extension

This will store the file either in the "bin" directory path or in the directory where the source-code is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM