FileUpload 不允许我保存
FileUpload does not let me to save
我有一个困惑的问题。我正在尝试使用 FileUpload。以下代码在本地运行良好:
var postedFile = uploader.PostedFile;
var fileName = Path.GetFileName(postedFile.FileName);
var extension = Path.GetExtension(fileName);
var newFile = Guid.NewGuid() + extension;
var imageFilePath = Path.Combine(this.Server.MapPath("~/ProductImages"), newFile);
uploader.SaveAs(imageFilePath);
但是当我将我的代码发布到我的互联网服务器时,出现了以下异常:
Could not find a part of the path [...]
当我将 uploader.SaveAs(imageFilePath);
更改为 uploader.SaveAs(imageFilePath.Replace(this.Request.ServerVariables["APPL_PHYSICAL_PATH"], "..\"));
时,出现此异常:
The SaveAs method is configured to require a rooted path, and the path [..] is not rooted.
谁能告诉我如何使用上传器?我该如何解决这个问题?
谢谢
问题是本地帐户在 Active Directory 上的远程资源上无效 - 您需要以 ComputerName$ 或 Anonymous 身份连接,具体取决于您使用的本地身份类型。
我可以在调试模式下上传,但不能在发布的网站上上传,所以这可能对你有帮助,穆罕默德。
您确定服务器上存在 ProductImages 文件夹吗?
您可以尝试拨打
Directory.CreateDirectory(this.Server.MapPath("~/ProductImages"))
保存文件之前。如果目录不存在,它将创建目录;如果目录存在,它将不执行任何操作。
终于通过给文件夹设置权限解决了问题。我为该文件夹设置了“应用程序池组”的写权限,现在我可以通过我的应用程序上传任何文件。
谢谢
你的代码没有问题。当您 运行 通过 Web 服务器运行应用程序时,Web 服务器由 OS 中的用户管理(例如 enginx 的 nobody 或 IIS 中的应用程序池组)。
您应该为要保存数据的文件夹设置权限。这可以在您的托管文件管理器面板中或通过直接访问 OS.
来完成
试试这个..上传和插入数据库中的值..
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("~/" + filename));
Stream str = FileUpload1.PostedFile.InputStream;
con.Open();
SqlCommand cmds = new SqlCommand("insert into datas(name,path,types,pricing)values(@name,@path,@types,@pricing)", con);
cmds.Parameters.AddWithValue("@name",d);
cmds.Parameters.AddWithValue("@path", filename);
cmds.Parameters.AddWithValue("@types", RadioButton1.Text);
cmds.Parameters.AddWithValue("@pricing", "FineGrained");
cmds.ExecuteNonQuery();
Response.Write("<script>alert('successfully uploaded');</script>");
我有一个困惑的问题。我正在尝试使用 FileUpload。以下代码在本地运行良好:
var postedFile = uploader.PostedFile;
var fileName = Path.GetFileName(postedFile.FileName);
var extension = Path.GetExtension(fileName);
var newFile = Guid.NewGuid() + extension;
var imageFilePath = Path.Combine(this.Server.MapPath("~/ProductImages"), newFile);
uploader.SaveAs(imageFilePath);
但是当我将我的代码发布到我的互联网服务器时,出现了以下异常:
Could not find a part of the path [...]
当我将 uploader.SaveAs(imageFilePath);
更改为 uploader.SaveAs(imageFilePath.Replace(this.Request.ServerVariables["APPL_PHYSICAL_PATH"], "..\"));
时,出现此异常:
The SaveAs method is configured to require a rooted path, and the path [..] is not rooted.
谁能告诉我如何使用上传器?我该如何解决这个问题?
谢谢
问题是本地帐户在 Active Directory 上的远程资源上无效 - 您需要以 ComputerName$ 或 Anonymous 身份连接,具体取决于您使用的本地身份类型。
我可以在调试模式下上传,但不能在发布的网站上上传,所以这可能对你有帮助,穆罕默德。
您确定服务器上存在 ProductImages 文件夹吗?
您可以尝试拨打
Directory.CreateDirectory(this.Server.MapPath("~/ProductImages"))
保存文件之前。如果目录不存在,它将创建目录;如果目录存在,它将不执行任何操作。
终于通过给文件夹设置权限解决了问题。我为该文件夹设置了“应用程序池组”的写权限,现在我可以通过我的应用程序上传任何文件。
谢谢
你的代码没有问题。当您 运行 通过 Web 服务器运行应用程序时,Web 服务器由 OS 中的用户管理(例如 enginx 的 nobody 或 IIS 中的应用程序池组)。 您应该为要保存数据的文件夹设置权限。这可以在您的托管文件管理器面板中或通过直接访问 OS.
来完成试试这个..上传和插入数据库中的值..
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("~/" + filename));
Stream str = FileUpload1.PostedFile.InputStream;
con.Open();
SqlCommand cmds = new SqlCommand("insert into datas(name,path,types,pricing)values(@name,@path,@types,@pricing)", con);
cmds.Parameters.AddWithValue("@name",d);
cmds.Parameters.AddWithValue("@path", filename);
cmds.Parameters.AddWithValue("@types", RadioButton1.Text);
cmds.Parameters.AddWithValue("@pricing", "FineGrained");
cmds.ExecuteNonQuery();
Response.Write("<script>alert('successfully uploaded');</script>");