(ObjectDisposedException) cannot upload FormFile to disk space [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to unit-test method
Here I instantiate new FormFile from disk
Here I use static method,with file and path to folder
var id= await ContentSaver.Save(file, path + "\\");
Than I try to save that file back to disk
And VS throw an exception "System.ObjectDisposedException : Cannot access a closed file."
How to solve it?
This is stacktrace

The answer at your first code part (https://i.stack.imgur.com/ktjip.png). You create the stream in using scope and dispose it at once. To avoid it copy the content of your file into MemoryStream (do not need to be deposed) and set as source of FormFile
IFormFile formFile;
using (var fstream = new FileStream("path", FileMode.Open))
{
var mstream = new MemoryStream();
fstream.CopyTo(mstream);
formFile = new FormFile(mstream, 0, mstream.Length, null, mstream.Name);
}
// here fstream is disposed, but not mstream, and you can use your FormFile instance
// MemoryStream does not need to be disposed explicitly, it do not posess any OS specific handlers, GC is enought.

Related

File.Exists returns false in UnitTest context [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have dumped a file with some data for UnitTesting.
When the code tries to load the file, it gets FileNotFoundException.
Data and code files next to each other in the same folder.
Project.Test
data.xml
data.cs
It appears that Test projects needs to copy the files after the build in the bin/Debug folder. It is only done if you set the "Copy to Output Directory" property to "Copy always".
I don't know if this would be appropriate for your purpose or not, but I'll often include sample/data/other files as embedded resources. If you choose to do this, you'll still need to set the Build Action property on the file in Solution Explorer, but then you don't need to worry about where a file is or if it exists.
Here's a sample method for reading the file. I'd suggest changing the method to return data as a string, XDocument, or other more-suitable format if it fits your data type. resource will be the project path to the file (i.e., Project.Test.data.xml in your example above).
private byte[] GetEmbeddedResourceBytes(string resource)
{
var asm = Assembly.GetExecutingAssembly();
using (var stream = asm.GetManifestResourceStream(resource)) {
if (stream != null) {
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream()) {
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) {
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
return new byte[0];
}

Open Resource File with FileStream fails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to open a Resource File with the FileStream Class. It's a text File and I want to read it Line by Line.
FileStream fs = new FileStream(Properties.Resources.Testing, FileMode.Open, FileAccess.Read);
The called Exception is System.ArgumentException and it says there is an invalid character.
I hope anyone can help me to fix this, or if theres a better way it's also ok but I need the File in the .exe so it needs to be a Resource..
When you add a text file as a resource, it will be embedded as a string. So your FileStream constructor call will assume that you are trying to open a file on disk with a name that is the same as the text file content. That ends poorly of course.
It isn't very clear if you really want a stream, the string tends to be good as-is, you might consider the String.Split() method to break it up into lines. Or maybe you like the StringReader class so you can use ReadLine():
using (var rdr = new StringReader(Properties.Resources.Testing)) {
string line;
while ((line = rdr.ReadLine()) != null) {
// Do something with line
//...
}
}

The Archive is either in unknown Format or Damaged using Dotnetzip library to zip file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am using below code to zip the memory stream on the fly dynamically and creating excel file inside that zipfile .....using dotnetzip dll ......
public ActionResult ExportToExcel()
{
byte[] file;
DataTable dt = common.CreateExcelFile.ListToDataTable(GetSearchDraftPRResults());
common.CreateExcelFile excelFileForExport = new CreateExcelFile();
file = excelFileForExport.CreateExcelDocumentAsStream(dt, targetFilename);
Response.Buffer = true;
var memStream = new MemoryStream(file);
var memoryStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddEntry("Generate-Excel.xlsx","", memStream);
zip.Save(memoryStream);
}
memStream.Seek(0, SeekOrigin.Begin);
return File(memoryStream, "application/octet-stream", "archive.zip");
}
I am getting the file created as zip file but when I click on archive.Zip file I am getting
error
ERROR
The Archive is either in unknown Format or Damaged
would any one please help on this why I am getting corrupted zip file when doing zip on fly that would be very grateful to me.
I have rectified my problem with this line memoryStream.Seek(0, SeekOrigin.Begin); replacing this one memStream.Seek(0, SeekOrigin.Begin);

C# - Extract one *.jar file into another one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to do my own Minecraft Launcher for Moded Minecraft. And I have a issue: I need to extract content of one *.jar file to another. I tried a lot of things and I am totally desperate.
Basicly, I need to do this in code.
As Andrew briefly mentioned, there are some good .NET libraries for manipulating zip files. I've found DotNetZip to be very useful, and there are lots of helpful worked examples here.
In answer to your question, if you just want to copy the contents of one *.jar file to another, keeping original content in the target file, please try the following. I'm using the .NET zip mentioned above (which also has a NuGet package!):
using (ZipFile sourceZipFile = ZipFile.Read("zip1.jar"))
using (ZipFile targetZipFile = ZipFile.Read("zip2.jar"))
{
foreach (var zipItem in sourceZipFile)
{
if (!targetZipFile.ContainsEntry(zipItem.FileName))
{
using (Stream stream = new MemoryStream())
{
// Write the contents of this zip item to a stream
zipItem.Extract(stream);
stream.Position = 0;
// Now use the contents of this stream to write to the target file
targetZipFile.AddEntry(zipItem.FileName, stream);
// Save the target file. We need to do this each time before we close
// the stream
targetZipFile.Save();
}
}
}
}
Hope this helps!

create txt file in selected location in c# application [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am developing a c# application. In this Form I have added 2 buttons.
Those are Browse and Create File Buttons.
Now I want to do is use browse button to browse a location and when click Create file button, create a text file in that location.
Have a look at
SaveFileDialog Class
Prompts the user to select a location for saving a file.
or
FolderBrowserDialog Class
Prompts the user to select a folder.
File.Create Method
Creates a file in the specified path.
or even
File.CreateText Method
Creates or opens a file for writing UTF-8 encoded text
on click event do it like this
//if you want to overwrite the file if it already exists you can bypass this check
if (File.Exists(path))
{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
if you don't intend to write anything
FileStream fs = File.Create(path);
fs.Close(); //this needs to be done
You need to read this.

Categories