Created
August 17, 2021 16:18
-
-
Save NickersF/382d2d590bd48f67e97543974ef596be to your computer and use it in GitHub Desktop.
Example of Binary Data no AJAX MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upload and transfer data to API/DB | |
[HttpPost] | |
public async Task<ActionResult> UploadFiles() | |
{ | |
// Checking no of files injected in Request object | |
if (Request.Files.Count > 0) | |
{ | |
try | |
{ | |
// Get all files from Request object | |
HttpFileCollectionBase files = Request.Files; | |
clientSchemaName = Request.Form.Get("selectedFuelImportSchema"); | |
for (int i = 0; i < files.Count; i++) | |
{ | |
HttpPostedFileBase file = files[i]; | |
string fname; | |
// Checking for Internet Explorer | |
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") | |
{ | |
string[] testfiles = file.FileName.Split(new char[] { '\\' }); | |
fname = testfiles[testfiles.Length - 1]; | |
} | |
else | |
{ | |
fname = file.FileName; | |
} | |
// Convert the file to a byte array for transport | |
BinaryReader b = new BinaryReader(file.InputStream); | |
FuelImportSendData fuelImportDataToApi = new FuelImportSendData(); | |
// Populate the viewmodel with the file contents and schema name and ship it | |
byte[] binData = b.ReadBytes(file.ContentLength); | |
// Get the fuel import schemas | |
List<FuelImportSchemaModel> fuelImportSchemaList = new List<FuelImportSchemaModel>(); | |
IEnumerable<object> fuelImportSchemas = await CoreService.LoadDataFromApi("FuelImportSchema", CoreService.GetToken()); | |
JsonConvert.PopulateObject(JsonConvert.SerializeObject(fuelImportSchemas), fuelImportSchemaList); | |
var result = Json(fuelImportSchemaList); | |
fuelImportDataToApi.File = binData; | |
fuelImportDataToApi.SchemaName = clientSchemaName; | |
SqlReturnModel retModel = new SqlReturnModel(); | |
retModel = await _coreService.InsertObject("FuelImport", fuelImportDataToApi, CoreService.GetToken()); | |
// Check if there was data that didn't pass and return that data. | |
if (retModel.FuelDataWithErrorJSON.HasValue()) | |
{ | |
//List<FuelImportParsedModel> parsedData = new List<FuelImportParsedModel>(); | |
JsonConvert.PopulateObject(retModel.FuelDataWithErrorJSON, parsedData); | |
return Json(parsedData.Count); //Number of Errors | |
} | |
// Get the complete folder path and store the file inside it. | |
//fname = Path.Combine(Server.MapPath("~/Uploads/"), fname); | |
//file.SaveAs(fname); | |
} | |
// Returns message that the file successfully uploaded | |
return Json("File Uploaded Successfully!"); | |
} | |
catch (Exception ex) | |
{ | |
return Json("Error occurred. Error details: " + ex.Message); | |
} | |
} | |
else | |
{ | |
return Json("No files selected."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment