【问题标题】:How to create a folder then save an image to the created folder using fileuploader in C# asp.net?如何创建一个文件夹,然后使用 C# asp.net 中的 fileuploader 将图像保存到创建的文件夹中?
【发布时间】:2013-07-12 10:50:52
【问题描述】:

我在这里有一个简单的程序,我可以在其中创建一个文件夹,然后将图像保存到创建的文件夹中。该文件夹已成功创建,但在将其保存到新创建的文件时出现错误。错误是:

无法上传文件。出现以下错误:'N:/Kim's New Project/Safety Accident Report/File Uploader 2/File Uploader 2/Uploads/asdsa' 是物理路径,但虚拟路径是 预计。

请检查我的代码。请帮忙。谢谢。

protected void button1_Click(object sender, EventArgs e)
        {
            if (FileUpload2.HasFile)
            {
                try
                {
                    if (FileUpload2.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUpload2.PostedFile.ContentLength < 512000)
                        {
                            string strpath = @"N:\Kim's New Project\Safety Accident Report\File Uploader 2\File Uploader 2\Uploads\" + txtName.Text;
                            if (!(Directory.Exists(strpath)))
                            {
                                Directory.CreateDirectory(strpath);
                                lblResult.Text = "Directory Created";
                                if ((Directory.Exists(strpath)))
                                {
                                    string filename = Path.GetFileName(FileUpload2.FileName);
                                    FileUpload2.SaveAs(Server.MapPath(strpath) + filename);
                                    Label1.Text = "File uploaded successfully!";
                                }
                            }
                            else
                            {
                                lblResult.Text = "Already Directory Exists with the same name";
                            }


                        }
                        else
                            Label1.Text = "File maximum size is 500 Kb";
                    }
                    else
                        Label1.Text = "Only JPEG files are accepted!";
                }
                catch (Exception exc)
                {
                    Label1.Text = "The file could not be uploaded. The following error occured: " + exc.Message;
                }
            }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    代替

    FileUpload2.SaveAs(Server.MapPath(strpath) + filename);
    

    试试

    FileUpload2.SaveAs(Path.Combine(strPath, filename));
    

    您已经知道物理保存路径 - 不需要 Server.MapPath

    【讨论】:

      【解决方案2】:

      试试..

      string strpath = Server.MapPath("~/Test");
      if (!(Directory.Exists(strpath)))
      {
          Directory.CreateDirectory(strpath);               
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        • 2023-04-04
        • 2017-05-18
        • 1970-01-01
        相关资源
        最近更新 更多