ren*_*thy 7 .net c# file-upload
我无法在.net c#中上传文件.我使用以下代码:
try
{
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Resources");
var dataStream = await file.ReadAsStreamAsync();
FileStream fileStream = File.Create(mappedPath, (int)dataStream.Length);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent("Successful upload", Encoding.UTF8, "text/plain");
response.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(@"text/html");
return response;
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message);
}
Run Code Online (Sandbox Code Playgroud)
这发生在localhost上.我有"访问路径...被拒绝"错误.我试图更改文件夹"资源"的安全权限,但可能我不知道添加完全控制的组/用户名.
我试过了:
小智 2
看起来您确实正在尝试使用mappedPath 分配覆盖目录,这可能是实际原因,而不是权限。
但是,就权限而言,鉴于您引用的是 IIS_IUSERS,看起来您正在 IIS 中工作。考虑向您的进程正在运行的身份授予写入权限;检查“进程模型”(应用程序池的高级设置)下的“身份”(IIS7+) 设置 - 如果是 ApplicationPoolIdentity,您需要将该身份添加到该文件夹的权限中。
即,如果您的应用程序池名为 my-app.com,则本地用户将为 [iis apppool\my-app.com]。