I am using Uploadify to upload multiple files in my ASP.NET MVC application. In the controller action, I need to check if one of the uploaded files is a zip file, and if yes, I need to check its contents. For the zip functionality I am using the ICSharpCode.SharpZipLib.
When uploading a zip file from say my desktop, I am getting the following error:
Could not find file ‘C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\xyz.zip’ on the following line of code:
FileStream fs = System.IO.File.OpenRead(Path.GetFullPath(fileData.FileName));
ZipFile zf = new ZipFile(fs);
How do I get past this error?
[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
if (fileData != null && fileData.ContentLength > 0)
{
if (Path.GetExtension(fileData.FileName) == ".zip")
{
FileStream fs = System.IO.File.OpenRead(Path.GetFullPath(fileData.FileName));
ZipFile zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
}
}
else
{
var fileName = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName));
fileData.SaveAs(fileName);
return Json(true);
}
}
return Json(false);
}
HttpPostedFileBase.FileNameis the name of the file uploaded, not the location on the file stored on the server.HttpPostedFileBasedoes not store the file on the server, only as a stream. Your options are either open the stream in memory (if your 3rd party utilies allow for opening streams) or saving the file to a known location, then open it from that location.