【发布时间】:2022-01-22 08:10:22
【问题描述】:
我正在尝试将文件上传到位于 ASP.NET Core 项目的托管服务器之外的文件夹。我尝试了不同的方法,但到目前为止都没有奏效。我也尝试使用 fttp 上传,但服务器提供商不允许这样做。这是我的代码
public async Task<IActionResult> DevicePhotoGalary(int id, IFormFile file) // IFormFile for one Photo and IFormCollection for Multi Photo
{
var QuestionObj = await _questionRepo.GetQuestionById(id);
if (QuestionObj == null)
return NotFound();
var uploadFolderPath = Path.Combine(_webHostEnvironment.WebRootPath, "IQ Questions"); //location must be changed here
if (!Directory.Exists(uploadFolderPath))
Directory.CreateDirectory(uploadFolderPath);
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
var filePath = Path.Combine(uploadFolderPath, fileName);
// var filePath = uploadFolderPath + fileName;
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
}
QuestionObj.Image = fileName;
await _questionRepo.UpdateQuestion(QuestionObj);
return Ok();
}
非常感谢任何帮助。谢谢
【问题讨论】:
-
我使用“AppContext.BaseDirectory”。在我的服务器上,它是 wwwroot 的父级。 (Inetpub)
-
如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
标签: asp.net asp.net-core asp.net-web-api