2011-03-25 63 views
0

嗨我有一个问题即时上传图片到图片都包含在〜/用户数据/用户名/ uploadedimage/image.jpg的陌生的字符串格式+路径从文件上传

我的项目路径我用下面的方法上传并将图片的路径存储在我的数据库中。

protected void UploadButton_Click(object sender, EventArgs e) 
    { 

     if (FileUploadControl.HasFile) 
     { 
      try 
      { 
       string theUserId = Session["UserID"].ToString(); 
       OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
       cn.Open(); 
       //string filename = Path.GetFileName(FileUploadControl.FileName); 
       string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName); 
       FileUploadControl.SaveAs(fileuploadpath); 
       StatusLabel.Text = "Upload status: File uploaded!"; 

       OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn); 
       cmd.ExecuteNonQuery(); 
      } 

      catch (Exception ex) 
      { 
       StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

      } 

     } 
    } 
} 

我发现什么是心不是我的项目目录的路径,该路径的一些奇怪的事情:

enter image description here

这里是我的数据库的一个片段的第一个idPictures = 1是正确的路径我需要的名字。

idPictures = 2是fileupload插入到我的数据库中的那个?

我怎样才能得到它,因此它会给出一个路径名这样的:

~/userdata/2/uploadedimage/batman-for-facebook.jpg 

编辑:

如果我试试这个:

string fileuploadpath = ("~/userdata/"+theUserId+"/uploadedimage/")+Path.GetFileName(FileUploadControl.FileName); 
      FileUploadControl.SaveAs(fileuploadpath); 
      StatusLabel.Text = "Upload status: File uploaded!"; 

      OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+theUserId+"','"+fileuploadpath+"')", cn); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

     } 

    } 
} 

    } 

我得到的错误:

档案cou ld不会被上传。发生以下错误:SaveAs方法被配置为需要一个根路径,并且路径'〜/ userdata/1/uploadedimage/holypally.jpg'没有根。

回答

1

我怀疑它是在您的SQL语句中将反斜杠当作转义字符。别忘了,您正在使用Server.MpaPath - 即您正在尝试为文件找到Windows绝对文件名。

这就是当您不使用参数化SQL语句时发生的情况,而是直接将您本质上用户指定的文本包含到您的SQL中。 不要这样做。使用参数化的SQL命令,分别指定值,然后至少不需要担心不合理的值。

当然,你仍然需要弄清楚真的是是否想存储翻译过的路径,但这是另一回事。

+0

呃,也许这样的侧隙因为idpictures 2缺少它们,但为什么它不像第一个图片路径那样保存,而是它给出了一个完整的路径 – 2011-03-25 11:57:02

+0

@Garrith:你调用'Server.MapPath'并插入结果作为'picturepath'的值。你确定你明白'Server.MapPath'的作用吗? – 2011-03-25 11:58:07

+0

不,我不想要翻译的路径和是的,我理解参数化的SQL语句,但ID喜欢先看到,如果我可以得到正确的路径名称,然后担心反斜杠 – 2011-03-25 12:00:28

0

为了回答我的问题我要创建两个字符串一个纯粹的文件上传和其他纯粹的数据库路径存储:

 string filenameDB = Path.GetFileName(FileUploadControl.FileName); 
     string fileuploadpath = Server.MapPath("~/userdata/"+theUserId+"/uploadedimage/")+Path.GetFileName(FileUploadControl.FileName); 
     FileUploadControl.SaveAs(fileuploadpath); 
     string fileuploadpaths = ("~/userdata/"+theUserId+"/uploadedimage/")+filenameDB; 
     StatusLabel.Text = "Upload status: File uploaded!"; 

     OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+theUserId+"','"+fileuploadpaths+"')", cn); 
     cmd.ExecuteNonQuery(); 
    } 

    catch (Exception ex) 
    { 
     StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

    } 

} 

}

} 
相关问题