I'm using ASP.NET 4.6 MVC. I'm saving an xml file as a byte array in my SQL db. I want to allow my user to download the file from the browser. I'm not getting any errors and no file is getting downloaded. I know there are plenty of these issues out there but I haven't seen one where a file isn't being downloaded (not even a corrupted one).
public ActionResult DownloadScript(int id) {
try {
var script = _db.PortalScripts.FirstOrDefault(i = >i.Id == id);
if (script != null) {
return File(script.ScriptBytes, "text/xml", script.Name);
}
}
catch(Exception e) {
FlashMessage.Danger("Error downloading script");
}
return RedirectToAction("Scripts");
}
[HttpPost]
public ActionResult UploadScript(HttpPostedFileBase file) {
try {
if (file.ContentLength > 0) {
var newScript = new PortalScript {
Name = Path.GetFileName(file.FileName),
Version = "190.161",
Description = "This is a placeholder",
Displayed = true
};
using(MemoryStream ms = new MemoryStream()) {
file.InputStream.CopyTo(ms);
newScript.ScriptBytes = ms.ToArray();
}
var existingScripts = _db.PortalScripts.FirstOrDefault(s = >s.ScriptBytes == newScript.ScriptBytes);
if (existingScripts == null) {
_db.PortalScripts.AddOrUpdate(newScript);
_db.SaveChanges();
FlashMessage.Confirmation(newScript.Name + " successfully uploaded");
}
else FlashMessage.Warning(newScript.Name + " already exists. Duplicate upload ignored.");
}
}
catch(Exception ex) {
FlashMessage.Danger("Upload failed: " + ex.Message);
}
return RedirectToAction("Scripts");
}
In my download method, my script variable is returning a properly filled out model. Not going into the catch either. Just getting redirected back to my original view with no file. Any suggestions??
Related
I have angular project as my client side and .Net 6 web api project as my backend. I am still new to both technologies. I am creating a website and there is a functionality that I am trying to add and haven't been successful so far. I want to upload images into a .Net web api project images folder using angular. I also want to later access those images from angular project. I want to store the path of the image files in the database. I have tried to check for the code on the internet without success. Your assistance will be appreciated.
First, submit your's files from frontend with FormData
postWithFile(url: string, obj: any, files: File[]) {
let cloneHeader: any = {};
let options: any = {
headers: new HttpHeaders(cloneHeader),
observe: 'response',
responseType: 'json'
};
let formData: FormData = new FormData();
if (typeof obj == 'object') { // obj is external submit data
formData.append('data', JSON.stringify(obj));
} else {
formData.append('data', obj);
}
if (files && files.length > 0) {
files.forEach((ds, index) => {
formData.append('file_' + index, ds, ds.name);
});
}
return this._http
.post(this.host + url, formData, options)
.pipe(map((res: any) => {
return res.body;
}));
}
And backend handle request with HttpContext.Current.Request.Files, save images to server and store path of images in database
[HttpPost]
public ResponseMessage<bool?> UploadImages()
{
var response = new ResponseMessage<bool?>();
try
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
ExternalDataModel model = MessageConvert.DeserializeObject<ExternalDataModel>(HttpContext.Current.Request["data"]); // obj in frontend
//
List<string> listImages = new List<string>();
DateTime now = DateTime.Now;
string buildPath = $"{string.Format("{0:00}", now.Year)}\\{string.Format("{0:00}", now.Month)}\\{string.Format("{0:00}", now.Day)}"; // change by your's folder path
foreach (string file in HttpContext.Current.Request.Files)
{
var fileContent = HttpContext.Current.Request.Files[file];
int fileLength = fileContent.ContentLength;
if (fileContent != null && fileLength > 0)
{
var stream = fileContent.InputStream;
byte[] imgByteArray;
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
imgByteArray = memoryStream.ToArray();
}
string fileName = $"format_file_name_if_need_{fileContent.FileName}";
string RelativeFolder = $"{buildPath}";
string AbsoluteFolder = Path.Combine("FOLDER_IN_SERVER_FULL_PATH", RelativeFolder);
if (!Directory.Exists(AbsoluteFolder))
{
Directory.CreateDirectory(AbsoluteFolder);
}
string pathSave = Path.Combine(RelativeFolder, fileName);
FileHelper.SaveFileFromBinaryArray(pathSave, imgByteArray);
listImages.Add(pathSave);
}
}
// model.listImages = listImages; // assign to model to save to DB
//
// var data = _bus.uploadImage(model);
// if (data)
// {
// response.Data = true;
// response.MessageCode = MessageCodes.UpdateSuccessfully;
// }
}
catch (Exception ex)
{
response.MessageCode = ex.Message;
}
return response;
}
I'm working on an ASP.NET Core 5 project. I have this action method:
public async Task<IActionResult> CreateV3EnterCheckFile(IFormFile MarksFile)
{
var filesCount = Directory.GetFiles("Uploads").Length;
string path = Path.Combine("Uploads", filesCount + 1 + ".xlsx");
await MarksFile.SaveToAsync(path);
var xlImporter = new XLImporter();
var importedData = await xlImporter.ImportSheetAsync(path, 0);
var r = (from x in importedData select new { ID = x[0], StudentId = x[1] }).ToList();
System.IO.File.Delete(path);
return View();
}
I tried to get IFormFile uploaded file by the user to save it on the server and querying it using one of my projects (that uses LinqToExcel library).
I am querying the data and everything is perfect I still have just one problem it is this line of code:
System.IO.File.Delete(path);
It throws an exception and the message is I can't delete that file because it is still being used by another process.
I'm very sure that the process is related to the LinqToExcel library.
More details :
SaveToAsync is an extension method created by me that is its definition
public static Task SaveToAsync(this IFormFile file, string pathToSaveTo)
{
return Task.Factory.StartNew(() =>
{
using (Stream fileStream = File.Open(pathToSaveTo, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
file.CopyTo(fileStream);
}
});
}
Please - is there any way or method or solution to delete this file even if it is being used by another process?
Massive thanks in advance.
Based on the source code of ExcelQueryFactory (https://github.com/paulyoder/LinqToExcel/blob/master/src/LinqToExcel/ExcelQueryFactory.cs) I would try the following:
ExcelQueryFactory has a ReadOnly Property. For read only access (if applicable) I would set it to true when creating the instance.
More important: IExcelQueryFactory implements IDisposable, so you can (should) use a using block:
using (var excelFile = new ExcelQueryFactory(pathToExcelFile) {ReadOnly = true})
{
// Do your work.
}
Of course you can use using var ..., but if you need a more reduced scope, the "old" using syntax allows more control.
I assumed that your Uploads folder is under webroot.
You can try this:-
public YourControllerName(IHostingEnvironment he) //input parameter
{
_he = he;
}
public async Task<IActionResult> CreateV3EnterCheckFile(IFormFile MarksFile)
{
try
{
var filesCount = Directory.GetFiles("Uploads").Length;
string contentRootPath = _he.ContentRootPath;
string path = Path.Combine(contentRootPath +"\\Uploads", filesCount + 1 + ".xlsx");
await MarksFile.SaveToAsync(path);
var xlImporter = new XLImporter();
var importedData = await xlImporter.ImportSheetAsync(path, 0);
var r = (from x in importedData select new { ID = x[0], StudentId = x[1] }).ToList();
//System.IO.File.Delete(path);
if (File.Exists(path))
{
File.Delete(path);
}
else
{
Debug.WriteLine("File does not exist.");
}
return View();
}
catch(Exception e)
{
Console.WriteLine(e);
}
Or you can try another process:-
try
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(path);
}
catch(Exception e){
}
}
Or this:-
if (System.IO.File.Exists(path))
{
try
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(path);
}
catch (Exception e) { }
}
it should resolve your issue I hope. by the way, if your Upload folder is not under the webroot path. you can find your path using your process.
Can't validate if is the correct file, the code breaks but file passes always.
[BindProperty, Required(ErrorMessage = "Please select a file!"), Attachment]
public IFormFile Upload { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
if (Upload != null && Upload.Length > 0)
{
var myFile = Path.Combine(env.ContentRootPath, folderName, Upload.FileName);
var fileExt = Path.GetExtension(Upload.FileName).Substring(1);
if (!supportedTypes.Contains(fileExt))
{
ModelState.AddModelError(string.Empty,
"Only Excel files are permited");
}
else
{
if (myFile.Length > 0)
{
using (var fileStream = new FileStream(myFile, FileMode.Create))
{
await Upload.CopyToAsync(fileStream);
fileStream.Close();
var stream = System.IO.File.Open(myFile, FileMode.Open, FileAccess.Read);
using (var excelStream = ExcelReaderFactory.CreateReader(stream))
{
AddExcelToDB(excelStream);
}
}
}
}
}
else
{
ModelState.AddModelError(string.Empty,
"Please, you must select a file");
}
return RedirectToPage("/Analises/Index");
}
private void AddExcelToDB(IExcelDataReader excelReader)
{
var excelTable = excelReader.AsDataSet().Tables[0];
for (var i = 1; i < excelTable.Rows.Count; i++)
{
//check if the header = the database
if (!((string)excelTable.Rows[0].ItemArray[0]).Contains("Name")
& !((string)excelTable.Rows[0].ItemArray[1]).Contains("Number")
& !((string)excelTable.Rows[0].ItemArray[3]).Contains("Date"))
{
ModelState.AddModelError("Error",
"Header mismatch, please select correct file");
break;
}
var name= (string)excelTable.Rows[i].ItemArray[0];
var number= (int)excelTable.Rows[i].ItemArray[1];
var notif = (string)excelTable.Rows[i].ItemArray[2];
var date= (DateTime)excelTable.Rows[i].ItemArray[3];
TestTable currentExcel = new TestTable
{
Name= name,
Num= number,
Notif = notif,
Date= date
};
this.db.TestTables.Add(currentExcel);
}
this.db.SaveChanges();
}
If the correct file is posted it is inserted correctly in the database and displayed in the /Analises/Index page.
But if a empty file or a file that does not match is uploaded it breaks (doesn't insert) but still redirects without the error message.
Maybe a fresh pair of eyes can point me in the right direction.
Thanks in advance!
I have some data to save into a database.
I have created a web api post method to save data. Following is my post method:
[Route("PostRequirementTypeProcessing")]
public IEnumerable<NPAAddRequirementTypeProcessing> PostRequirementTypeProcessing(mdlAddAddRequirementTypeProcessing requTypeProcess)
{
mdlAddAddRequirementTypeProcessing rTyeProcessing = new mdlAddAddRequirementTypeProcessing();
rTyeProcessing.szDescription = requTypeProcess.szDescription;
rTyeProcessing.iRequirementTypeId = requTypeProcess.iRequirementTypeId;
rTyeProcessing.szRequirementNumber = requTypeProcess.szRequirementNumber;
rTyeProcessing.szRequirementIssuer = requTypeProcess.szRequirementIssuer;
rTyeProcessing.szOrganization = requTypeProcess.szOrganization;
rTyeProcessing.dIssuedate = requTypeProcess.dIssuedate;
rTyeProcessing.dExpirydate = requTypeProcess.dExpirydate;
rTyeProcessing.szSignedBy = requTypeProcess.szSignedBy;
rTyeProcessing.szAttachedDocumentNo = requTypeProcess.szAttachedDocumentNo;
if (String.IsNullOrEmpty(rTyeProcessing.szAttachedDocumentNo))
{
}
else
{
UploadFile();
}
rTyeProcessing.szSubject = requTypeProcess.szSubject;
rTyeProcessing.iApplicationDetailsId = requTypeProcess.iApplicationDetailsId;
rTyeProcessing.iEmpId = requTypeProcess.iEmpId;
NPAEntities context = new NPAEntities();
Log.Debug("PostRequirementTypeProcessing Request traced");
var newRTP = context.NPAAddRequirementTypeProcessing(requTypeProcess.szDescription, requTypeProcess.iRequirementTypeId,
requTypeProcess.szRequirementNumber, requTypeProcess.szRequirementIssuer, requTypeProcess.szOrganization,
requTypeProcess.dIssuedate, requTypeProcess.dExpirydate, requTypeProcess.szSignedBy,
requTypeProcess.szAttachedDocumentNo, requTypeProcess.szSubject, requTypeProcess.iApplicationDetailsId,
requTypeProcess.iEmpId);
return newRTP.ToList();
}
There is a field called 'szAttachedDocumentNo' which is a document that's being saved in the database as well.
After saving all data, I want the physical file of the 'szAttachedDocumentNo' to be saved on the server. So i created a method called "UploadFile" as follows:
[HttpPost]
public void UploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
// Get the uploaded file from the Files collection
var httpPostedFile = HttpContext.Current.Request.Files["UploadedFile"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
string folderPath = HttpContext.Current.Server.MapPath("~/UploadedFiles");
//string folderPath1 = Convert.ToString(ConfigurationManager.AppSettings["DocPath"]);
//Directory not exists then create new directory
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// Get the complete file path
var fileSavePath = Path.Combine(folderPath, httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
}
}
}
Before running the project, i debbugged the post method, so when it comes to "UploadFile" line, it takes me to its method.
From the file line, it skipped the remaining lines and went to the last line; what means it didn't see any file.
I am able to save everything to the database, just that i didn't see the physical file in the specified location.
Any help would be much appreciated.
Regards,
Somad
Makes sure the request "content-type": "multipart/form-data" is set
[HttpPost()]
public async Task<IHttpActionResult> UploadFile()
{
if (!Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
try
{
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
if (provider.Contents != null && provider.Contents.Count == 0)
{
return BadRequest("No files provided.");
}
foreach (HttpContent file in provider.Contents)
{
string filename = file.Headers.ContentDisposition.FileName.Trim('\"');
byte[] buffer = await file.ReadAsByteArrayAsync();
using (MemoryStream stream = new MemoryStream(buffer))
{
// save the file whereever you want
}
}
return Ok("files Uploded");
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
I've successfully made upload/download calls with the vanilla web api, but now I am trying to consolidate my logic on ODataControllers, so making these upload/download calls part of the same controller that returns the list of files.
To do this I made an action:
[HttpPost]
public Task<IEnumerable<FileComponent>> Upload()
{
try
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var provider = new MultipartFormDataStreamProvider(root);
return Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<IEnumerable<FileComponent>>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, t.Exception));
}
var fileComponents = new List<FileComponent>();
foreach (MultipartFileData file in provider.FileData)
{
string filename = file.Headers.ContentDisposition.FileName.Replace("\"", "");
var filepath = root + filename;
if (File.Exists(filepath))
{
// If previous entry exists, delete
File.Delete(filepath);
}
// The file is saved with a random GUID for name. Can rename it here.
string guidFilepath = file.LocalFileName;
File.Move(guidFilepath, filepath);
fileComponents.Add(new FileComponent()
{
Name = filename,
Size = new FileInfo(filepath).Length
});
}
return fileComponents;
});
}
finally
{
log.Info("Exiting Post");
}
}
However it never works. It always arrives with no FormData populating in the MultipartFormDataStreamProvider, and the exception is caught and returned as 'Unexpected end of MIME multipart stream. MIME multipart message is not complete.'.
Any ideas? I'm assuming OData is cleaning it.