Load stream in Epplus causes disk write error - asp.net-mvc-4

Im upload an excel file and trying to load it into a ExcelPackage. Like this
public static void ImportStuff(Stream stream)
{
using (ExcelPackage package = new ExcelPackage())
{
package.Load(stream);
...
My calling method gets the stream from an uploaded file
public async Task<ActionResult> UploadCsv(int Id, HttpPostedFileBase myfile)
{
ImportProducts(myfile.InputStream);
}
This will cause a A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))
Why do a get a write error when I am trying to load something?

I was trying to upload a csv file. Not a xslx file. Could have hoped for a more descriptive error message but it is working.

Related

Epplus - Reading through filestream. The process cannot access the file because it is being used by another process. -

I’m having an issue using EPPlus 6.0.6 on the server within an IIS process. I upload an xlsx to the IIS server, then call the method to process the file (read from the file only.) When finished, I want to remove the file from the server folder, but get the error:
The process cannot access the file because it is being used by another process.
Calling method:
Processor processor = new Processor (filename)
{
Await processor.Process();
try { File.Delete(selectedFile); txtStatusMsg += “Cleanup Succeeded”;}
catch (Exception e){ txtStatusMsg += $#"Cleanup Failed - {e.Message}"; }
// error occurs
// ex.message is
// The process cannot access the file because it is being used by another process.
}
The Process method has:
using (FileStream fs =
new FileStream(filename ,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (ExcelPackage srcPkg = new ExcelPackage(fs))
{
// read from the file
foreach (ExcelWorksheet srcWs in srcPkg.Workbook.Worksheets) ...
}
}
The file is still locked; I would expect the srcPkg to be disposed after the using block, and fs to be closed/disposed after its using block.
I’ve tried
srcPkg.Dispose();
after the for loop, and
fs.Close();
After the srcPkg using but neither help. Restarting IIS releases the file.
Can i force the file to be unlocked in some way?

Reading static files in ASP.NET Blazor

I have a client side Blazor Application. I want to have an appsetting.json file for my client-side configuration like we have an environment.ts in
Angular.
For that, I am keeping a ConfigFiles folder under wwwroot and a JSON file inside of it. I am trying to read this file as below.
First get the path:
public static class ConfigFiles
{
public static string GetPath(string fileName)
{
return Path.Combine("ConfigFiles", fileName);
}
}
Than read it:
public string GetBaseUrl()
{
string T = string.Empty;
try
{
T = File.ReadAllText(ConfigFiles.GetPath("appsettings.json"));
}
catch (Exception ex)
{
T = ex.Message;
}
return T;
}
But I always get the error:
Could not find a part of the path "/ConfigFiles/appsettings.json".
Inside the GetPath() method, I also tried:
return Path.Combine("wwwroot/ConfigFiles", fileName);
But I still get the same error:
Could not find a part of the path "wwwroot/ConfigFiles/appsettings.json".
Since there is no concept of IHostingEnvironmentin client-side Blazor, what is the correct way to read a static JSON file here?
I have a client side Blazor Application
OK, That means that File.ReadAllText(...) and Path.Combine(...) are not going to work at all.
Client-side means that you could be running on Android or Mac-OS or whatever.
The Blazor team provides you with a complete sample of how to read a file, in the form of the FetchData sample page.
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
this gets you the contents of a file in wwwroot/sample-data
You can use Http.GetStringAsync(...) if you want AllText
If you want to have per-user settings then look into the Blazored.LocalStorage package.

DropBox does not auto rename file during upload

I'm using Dropbox Java API to upload file.
Following code does not upload file with auto rename:
void uploadFile(#NonNull final String inPath, #NonNull final InputStream inputStream) throws IOException, DbxException {
client.files()
.uploadBuilder(inPath)
.withMode(WriteMode.ADD)
.withAutorename(true)
.uploadAndFinish(inputStream);
}
Can you please suggest what am I doing wrong?
The answer is that:
Dropbox does not consider there to be a conflict if you upload content identical to what's already in that file, and the rename will only occur on conflict.

how to download exe files in vb.net (Visual Studio 2015)

I try make one program for download one .exe file and run for help in my job.
But idk how to make this, i'm new in VB.
I am using this code, as shown in the Visual Basic document reference:
My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/WineList.txt", _
"C:\Documents and Settings\All Users\Documents\WineList.txt")
But when I try to download an .exe file, the entire file doesn't complete and I the file is only 1 kb after download.
The webclient should be the way to go a comment above highlights that too.
This is an example from another question:
Either use sync method:
public void DownloadFile()
{
using(var client = new WebClient())
{
client.DownloadFile(new Uri("http://www.FileServerFullOfFiles.net/download/test.exe"), "test.exe");
}
}
Or use new async-await approach:
public async Task DownloadFileAsync()
{
using(var client = new WebClient())
{
await client.DownloadFileTaskAsync(new Uri("http://www.FileServerFullOfFiles.net/download/test.exe"), "test.exe");
}
}
Then call this method like this:
await DownloadFileAsync();
Open up the .exe file you are trying to download in a text editor like NotePad. Odds are what is being downloaded is an HTML page showing some kind of error message like 404 not found.
Another possibility might be that AntiVirus software is moving the original EXE into quarantine and replacing it with a Quarantine MetaData file.
If the file does actually contain binary content your connection could be getting interrupted but odds are if this happened an exception would be thrown.

Replacing files throws UnauthorizedAccessException in WinRT / Win8

Every once in a while, StorageFiles get locked and I get an UnauthorizedAccessException when trying to overwrite them. I cannot replicate this, it only happens randomly. This is the code for creating files:
using (var stream = new MemoryStream())
{
// ...populate stream with serialized data...
StorageFile file;
Stream fileStream;
try
{
file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
}
catch (UnauthorizedAccessException ex)
{
Debug.WriteLine("Access denied on file {0}", fileName);
return;
}
fileStream = await file.OpenStreamForWriteAsync();
using (fileStream)
{
stream.Seek(0, SeekOrigin.Begin);
await stream.CopyToAsync(fileStream);
await fileStream.FlushAsync();
}
}
Once a file starts throwing UnauthorizedAccessException, it will always throw it. As if the system has the file locked and I cannot touch it. I have to uninstall the application and rebuild.
When I open the file in my document, I can see that data there. Everything is fine. It was written successfully.
Can anyone see a problem with my code?
Are you saving the file token in the future access list? I ran into this problem when loading files and trying to save updates later. Once I started using the future access list, the problems went away.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.accesscache.storageitemaccesslist
It might be the case when the same file is being accessed from two different points in the code at the same time.