How To Accept a File POST

前端 未结 13 1618
深忆病人
深忆病人 2020-11-22 09:48

I\'m using asp.net mvc 4 webapi beta to build a rest service. I need to be able to accept POSTed images/files from client applications. Is this possible using the webapi?

13条回答
  •  时光取名叫无心
    2020-11-22 10:08

    Complementing Matt Frear's answer - This would be an ASP NET Core alternative for reading the file directly from Stream, without saving&reading it from disk:

    public ActionResult OnPostUpload(List files)
        {
            try
            {
                var file = files.FirstOrDefault();
                var inputstream = file.OpenReadStream();
    
                XSSFWorkbook workbook = new XSSFWorkbook(stream);
    
                var FIRST_ROW_NUMBER = {{firstRowWithValue}};
    
                ISheet sheet = workbook.GetSheetAt(0);
                // Example: var firstCellRow = (int)sheet.GetRow(0).GetCell(0).NumericCellValue;
    
                for (int rowIdx = 2; rowIdx <= sheet.LastRowNum; rowIdx++)
                   {
                      IRow currentRow = sheet.GetRow(rowIdx);
    
                      if (currentRow == null || currentRow.Cells == null || currentRow.Cells.Count() < FIRST_ROW_NUMBER) break;
    
                      var df = new DataFormatter();                
    
                      for (int cellNumber = {{firstCellWithValue}}; cellNumber < {{lastCellWithValue}}; cellNumber++)
                          {
                             //business logic & saving data to DB                        
                          }               
                    }
            }
            catch(Exception ex)
            {
                throw new FileFormatException($"Error on file processing - {ex.Message}");
            }
        }
    

提交回复
热议问题