protected void btnImport_Click(object sender, System.EventArgs e) { bool error = false; string copiedFileName = ""; string submittedFileName = ""; if (txtFile.PostedFile.ContentLength > 0) { // Check content type - should only be a VBF/XLS file switch (txtFile.PostedFile.ContentType) { case "application/octet-stream" : case "text/plain" : submittedFileName = txtFile.PostedFile.FileName; try { // Confirm the file is the correct size // Copy the file to the server copiedFileName = CopyFile(submittedFileName); // Import the file VBFFile v = new VBFFile(int.Parse(txtVehicleInstID.Text), copiedFileName, User.Identity.Name, int.Parse(cboModule.SelectedItem.Value)); string message = v.Import(out error); // Delete the file from the server DeleteFile(copiedFileName); // Display message or redirect as applicable string redirectUrl = "VehicleInstT.aspx?" + Constants.QueryString.VehicleInstId + "=" + txtVehicleInstID.Text; if (message != "") { lblMessage.Text = message; lnkRedirect.Text = "View Imported Data"; lnkRedirect.NavigateUrl = redirectUrl; } else { // Redirect to the main page Response.Redirect(redirectUrl); } } catch (System.Exception exception) { //Generate error and redirect Context.Items.Add("errorMessage", exception.Message); Context.Server.Transfer("~/Error.aspx"); } break; case "application/vnd.ms-excel" : { //If no language supplied raise an error Context.Items.Add("errorMessage", "Cannot Import. Excel import not yet available!"); Context.Server.Transfer("~/Error.aspx"); break; } default: //If no language supplied raise an error Context.Items.Add("errorMessage", "Cannot Import. Import file format invalid!"); Context.Server.Transfer("~/Error.aspx"); break; } } }
public static string ImportVBF(string fileName, string name, int vehicleLine, string user, int module) { bool flag = false; string result = ""; try { int vehicleInstId = DataAccess.AddVehicleInst(name, vehicleLine, "Imported " + DateTime.get_Now().ToString() + " by " + user, user); VBFFile vBFFile = new VBFFile(vehicleInstId, fileName, user, module); result = vBFFile.Import(out flag); if (!flag) { File.Delete(fileName); } } catch (Exception ex) { result = ex.get_Message(); } return result; }
public ArrayList OutputVBF(int vInstId, string outputFolder, string user, bool withSDS, bool withBackups, string fileName) { string text = ""; string text2 = ""; ArrayList arrayList = new ArrayList(); string protocol = ""; ArrayList arrayList2 = new ArrayList(); VehiclePrototype vehiclePrototype = new VehiclePrototype(vInstId); bool withSDS2 = withSDS && !vehiclePrototype.XCLSDS; IEnumerator enumerator = vehiclePrototype.Modules.GetEnumerator(); try { while (enumerator.MoveNext()) { VehicleModule vehicleModule = (VehicleModule)enumerator.get_Current(); if (withBackups || vehicleModule.IsMaster) { text2 = fileName.Replace(".vbf", "_" + vehicleModule.Name + ".vbf"); text = base.get_Server().MapPath(outputFolder + text2); string inputFile = base.get_Server().MapPath(Constants.Config.TemplateFolder + "/" + vehicleModule.VBFTemplate); VBFFile vBFFile = new VBFFile(vInstId, text, inputFile, user); vBFFile.VehicleName = vehiclePrototype.VehicleCode; vBFFile.PartNo = vehicleModule.PartNo; vBFFile.PartType = vehicleModule.PartType; vBFFile.Network = vehicleModule.Network; vBFFile.ECUName = vehicleModule.Name; vBFFile.ECUAddress = vehicleModule.ECUAddress; vBFFile.EraseStart = vehicleModule.EraseStart; vBFFile.NoOfBytes = vehicleModule.NoOfBytes; vBFFile.Protocol = vehicleModule.Protocol; vBFFile.SBLFilename = vehicleModule.SBLFilename; vBFFile.SecurityInterface = vehicleModule.SecurityInterface; vBFFile.Comment = string.Concat(new object[] { "\"", vehiclePrototype.Name, "\" - ", vBFFile.VehicleName, " ", vehicleModule.Name, " - Extracted ", DateTime.get_Now() }); if (vehicleModule.IsMaster) { protocol = vehicleModule.Protocol; } if (vehiclePrototype.VBFType == 1) { vBFFile.EraseLength = vehicleModule.EraseLength; vBFFile.Params = vehicleModule.Parameters; } else { ArrayList arrayList3 = new ArrayList(); IEnumerator enumerator2 = vehicleModule.BuildTypes.GetEnumerator(); try { while (enumerator2.MoveNext()) { BuildType buildType = (BuildType)enumerator2.get_Current(); arrayList3.Add(new VDFBlock(buildType.BlockType, buildType.NoOfBytes)); } } finally { IDisposable disposable = enumerator2 as IDisposable; if (disposable != null) { disposable.Dispose(); } } VDF vDF = new VDF(vehiclePrototype.VDFVersion, vehicleModule.Parameters, vehicleModule.EraseLengthStored, arrayList3); vBFFile.Params = vDF.Data; vehicleModule.EraseLength = "0x" + vBFFile.Params.get_Count().ToString("x8"); vBFFile.EraseLength = vehicleModule.EraseLength; } if (withSDS) { if (vehiclePrototype.XCLSDS) { string template = base.get_Server().MapPath(Constants.Config.TemplateFolder + "/" + vehicleModule.SDSTemplate); SDSNode sDSNode = new SDSNode(vehicleModule.Name, vehicleModule.ECUAddress, vehicleModule.SecurityInterface, vehicleModule.SBLFilename, text2, template); arrayList2.Add(sDSNode); arrayList.Add(new VBFLink(text2, outputFolder + text2, "Save VBF")); } else { vBFFile.SDSOutputFile = text.Replace(".vbf", ".sds"); vBFFile.SDSTemplateFile = base.get_Server().MapPath(Constants.Config.TemplateFolder + "/" + vehicleModule.SDSTemplate); arrayList.Add(new VBFLink(text2, outputFolder + text2, "Save VBF", text2.Replace(".vbf", ".sds"), outputFolder + text2.Replace(".vbf", ".sds"), "Save SDS")); } } else { arrayList.Add(new VBFLink(text2, outputFolder + text2, "Save VBF")); } vBFFile.Output(withSDS2); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } if (withSDS && vehiclePrototype.XCLSDS) { string text3 = fileName.Replace(".vbf", ".sds"); string outputFile = base.get_Server().MapPath(outputFolder + text3); string inputFile2 = base.get_Server().MapPath(Constants.Config.TemplateFolder + "/" + vehiclePrototype.SDSHeaderTemplate); SDSFile sDSFile = new SDSFile(vehiclePrototype.VehicleCode, protocol, outputFile, inputFile2, arrayList2); sDSFile.Output(); arrayList.Add(new VBFLink(text3, outputFolder + text3, "Save SDS")); } return arrayList; }