pro_karma
pro_karma

Reputation: 13

Upload zip file error

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);
        }

Upvotes: 0

Views: 2489

Answers (3)

Erik Philips
Erik Philips

Reputation: 54638

HttpPostedFileBase.FileName is the name of the file uploaded, not the location on the file stored on the server. HttpPostedFileBase does 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.

Upvotes: 1

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

If you want to check if it is a zip file, you could always look at the content-type coming back.

application/zip

This might work for what you are trying to do. Also, just try and look at what the content-type, you might find something more specific to your needs.

Upvotes: 0

SLaks
SLaks

Reputation: 887797

Path.GetFullPath gets a full path from the current directory.
That has nothing to do with your HttpUploadedFileBase, which isn't on disk.

You need to pass a stream instead of a file path.

Upvotes: 0

Related Questions