c# - 尝试将文件名添加到字符串末尾时出错 :

标签 c# asp.net mysql sql html

尝试将文件名添加到字符串末尾时出错:

 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/") + filename);
            // error on this line filename only assignment, call can be used as a statement
            FileUploadControl.SaveAs(Server.MapPath(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;

        }

    }
}

        }

如果我这样尝试:

        string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
        FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename));

我收到一个mysql错误,因为路径末尾没有添加文件名(顺便说一句,我只是想保存路径而不是图像)尽管从技术上讲我仍然应该能够将半个文件路径插入sql所以也许这错误与我使用上面代码的原始上传方法无关。但显然我仍然需要完整的路径名。

文件无法上传。发生以下错误: ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 ''C:\Users\Garrith\Documents\Visual Studio 2010\WebSites\WebSite1\userdata\1\uplo' 附近使用的正确语法

最佳答案

您在同一个字符串上使用了 Server.MapPath 两次。请将其从任何位置删除,以便根据服务器映射的路径不会再次映射。

string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
FileUploadControl.SaveAs(Server.MapPath(fileuploadpath));

你可以这样做...

string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
FileUploadControl.SaveAs(fileuploadpath);

关于c# - 尝试将文件名添加到字符串末尾时出错 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5428147/

相关文章:

c# - 使用 Windows 窗体实现接口(interface)

c# - KeyVaultClient.AuthenticationCallback Delegate 的参数来自哪里?

c# - 无法调用动态按钮上的 Click 事件

mysql - 从 DATETIME 获取日期。数据库

mysql内部从多个表多次连接同一个表和同一列

c# - .NET 4 中的多线程改进

c# - Silverlight XAP 文件未更新

asp.net - 内容处置 :What are the differences between "inline" and "attachment"?

c# - 如何在jquery中获取全局变量的值?

PHP - SQL 脚本创建器