【问题标题】:Upload multiple files & create zip上传多个文件并创建 zip
【发布时间】:2015-02-02 18:58:51
【问题描述】:

我成功上传了两种文件并将各自的文件路径存储在数据库中。现在,我还想创建一个附有两个文件的 zip。

我在这个特定网页上引用了有关 zip 文件和文件上传的代码 http://www.c-sharpcorner.com/uploadfile/5089e0/how-to-create-zip-in-asp-net-using-c-sharp/

单击提交按钮后,会引发 File Not Found 异常。 Ionic.Zip.dll 中出现“System.IO.FileNotFoundException”类型的异常,但未在用户代码中处理

附加信息:C:\Users\seldarine\Desktop\Proj\PP_PJ\PP_Project\SFiles\Submissions\blueteam\MyCodesF.zip

在这一行抛出异常 - objZip.Save(Path.Combine(serverFilePath,zipName));

这是我的部分代码:

  protected void btn_submit_Click(object sender, EventArgs e)
    {
        string input = "";
        string pp, vv,zip;
        string extPp, extVv;

        if (f_slide.HasFile && f_video.HasFile)
        {
            //Get file name & extensions
            pp = Path.GetFileName(f_slide.PostedFile.FileName);
            extPp = Path.GetExtension(pp);
            vv = Path.GetFileName(f_video.PostedFile.FileName);
            extVv = Path.GetExtension(vv);

            string user = Session["userid"].ToString();
            //where zip name is named after the username (current user)  logged in
            string zipName = user + ".zip";

            //To save to folders
            string filePath = "SFiles/Submissions/" + user + "/";
            string serverFilePath = Server.MapPath(filePath);

            //If directory does not exist
            if (!Directory.Exists(serverFilePath))
            { // if it doesn't exist, create
                System.IO.Directory.CreateDirectory(serverFilePath);
            }

            //****To create zip file*****
            using(ZipFile objZip = new ZipFile())
            {
                string zipSlidePath = Path.Combine(serverFilePath,pp);
                string zipVideoPath = Path.Combine(serverFilePath,vv);
                objZip.AddFile(zipSlidePath);
                objZip.AddFile(zipVideoPath);
                ***objZip.Save(Path.Combine(serverFilePath,zipName));***
            }

            //Store files
            f_slides.SaveAs(Path.Combine(serverFilePath, pp));
            f_video.SaveAs(Path.Combine(serverFilePath, vv));

            .....

【问题讨论】:

  • 您发布的文件是否真的存储在这里:“SFiles/Submissions/”+用户+“/”?
  • 是的,文件存储在那里。我觉得语法有问题,在抛出异常的那一行。

标签: c# asp.net file-upload filepath dotnetzip


【解决方案1】:

尝试在崩溃的上面添加另一行,看看是否可以调试它以及得到什么结果:

var savePath = Path.Combine(serverFilePath,zipName +".zip"); 

您可能还想尝试将 savePath 变量更改为类似 c:\temp\test.zip 的内容,看看是否可行。

var savePath = "c:\\temp\\test.zip";

【讨论】:

  • 相同的异常结果。异常:抛出:(System.IO.FileNotFoundException)
  • 你能把异常的全文贴出来吗?
【解决方案2】:

确保运行服务器的用户具有对该文件夹 (serverFilePath) 的写入权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多