简体   繁体   中英

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM