使用HTML-FileUpload-Control - Access Denied上传文件
本文关键字:文件 Denied Access HTML-FileUpload-Control 使用 | 更新日期: 2023-09-27 17:49:27
单击我的Import-Button执行以下JQuery-Code:
if ($("#fileUpload").val()) {
$.ajax({
async: false,
url: "Handler/Handler.ashx?op=Import",
data: JSON.stringify(myData),
type: 'POST',
contentType: 'application/json; charset=utf-8'
});
myData
是一个json对象,包含HTML元素的文件路径和其他一些值。
当这个语句到达…
using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite))
…这是运行的异常:
UnauthorizedAccessException - Access to the path XXX is denied.
我可以通过给IUSER完全控制权限来解决这个问题。但是,这不是一个选项,因为我们不能期望最终用户每次想要上传文件时都修改权限。有没有其他不涉及终端用户输入的方法?
如果我没理解错的话,你可以在使用前给予许可。
private bool GrantAccess(string filepath)
{
FileInfo fInfo = new FileInfo(filepath);
FileSecurity fSecurity = fInfo.GetAccessControl();
fSecurity.AddAccessRule(new FileSystemAccessRule("IUSER",
FileSystemRights.FullControl,
AccessControlType.Allow));
fInfo.SetAccessControl(fSecurity);
return true;
}
和using-block
之后private bool RemoveAccess(string filepath)
{
FileInfo fInfo = new FileInfo(filepath);
FileSecurity fSecurity = fInfo.GetAccessControl();
fSecurity.AddAccessRule(
new FileSystemAccessRule("IUSER",
FileSystemRights.FullControl,
AccessControlType.Deny));
fInfo.SetAccessControl(fSecurity);
return true;
}
希望有帮助。
不要忘记包括using System.IO;