I got the following error while uploading a file using FileUpload control. I solved by getting the file contents in bytes so you can assign to it.
public void Compress()
{
string fileWithExt = Path.GetFileName(FileUpload1.FileName);
string zipFile = fileWithExt + ".zip";
string zipFilePath = Path.Combine(Server.MapPath("~/Uploads"), zipFile);
using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
zipstream.Write(data, 0, data.Length);
zipstream.Flush();
}
}
Exception message:
Solution:
You just replace the above code with the highlighted code.
byte[] fileBytes = FileUpload1.FileBytes;
using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = fileBytes;
zipstream.Write(data, 0, data.Length);
zipstream.Flush(); }
Post your comments / questions
Recent Article
- How to use if else statement in c++?
- How to use godaddy domain name with another godaddy hosting account?
- Restore of database 'DATABASE' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
- How to programmatically modifying the AppSetting value in web.config using c#?
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
- How to calculate the age from jQuery ui datepicker using c#?
- How to calculate days difference between two dates in c#?
- Changing date format in jQuery ui datepicker
Related Article