iis - 上传图片但大小为 0 KB
问题描述
我正在使用 .NET Core 3.1,但遇到了一个奇怪的问题!问题是任何上传的图像,当我重新启动 IIS 并再次尝试上传它时,它的大小为 0KB,没有任何问题,但在那之后,问题又回来了。
我通过使我的代码异步尝试了这个解决方案,但没有运气
我将我的系统文件更改为我的应用程序池用户可以访问但没有运气
这是我的代码:
public Document shareWithUsers([FromForm] CreateDocumentDto documentDto)
{
List<CreateUserType1DocumentsDto> listOfCreateUserType1Documents = new List<CreateUserType1DocumentsDto>();
List<CreateHDDocumentsDto> listOfCreateHDDocuments = new List<CreateHDDocumentsDto>();
if (documentDto.listOfUserType1Documents != null)
{
foreach (var item in documentDto.listOfUserType1Documents)
{
listOfCreateUserType1Documents.Add(JsonConvert.DeserializeObject<CreateUserType1DocumentsDto>(item));
}
}
else if (documentDto.listOfHDDocuments != null)
{
foreach (var item in documentDto.listOfHDDocuments)
{
listOfCreateHDDocuments.Add(JsonConvert.DeserializeObject<CreateHDDocumentsDto>(item));
}
}
if (documentDto.sharedText == null)
{
if (documentDto.document != null) //that mean user upload file
{
if (documentDto.document.Length > 0)
{
var UploadedFilesPath = Path.Combine(hosting.WebRootPath/*wwwroot path*/, "UploadedFiles" /*folder name in wwwroot path*/);
var filePath = Path.Combine(UploadedFilesPath, documentDto.document.FileName);
//documentDto.docUrl = filePath;
var documentObject = new Document();
using (var stream = new FileStream(filePath, FileMode.Create))
{
documentDto.document.CopyToAsync(stream);// Use stream
}
if (documentDto.listOfUserType1Documents != null)
{
documentObject.listOfUserType1Documents = ObjectMapper.Map<List<UserType1Documents>>(listOfCreateUserType1Documents);
}
else if (documentDto.listOfHDDocuments != null)
{
documentObject.listOfHDDocuments = ObjectMapper.Map<List<HDDocuments>>(listOfCreateHDDocuments);
}
documentObject.docTtitle = documentDto.docTtitle;
documentObject.docName = documentDto.docName;
documentObject.docUrl = filePath;
return _repository.Insert(documentObject);
}
}
}
else
{ //that mean user upload text
var documentObject = new Document();
if (documentDto.listOfUserType1Documents != null)
{
documentObject.listOfUserType1Documents = ObjectMapper.Map<List<UserType1Documents>>(listOfCreateUserType1Documents);
}
else if (documentDto.listOfHDDocuments != null)
{
documentObject.listOfHDDocuments = ObjectMapper.Map<List<HDDocuments>>(listOfCreateHDDocuments);
}
documentObject.sharedText = documentDto.sharedText;
return _repository.Insert(documentObject);
}
return null;
}
解决方案
我的代码只有在我将它们(CopyTo 和 Insert)都更改为 Async with await 时才有效
等待 CopyToAsync()
等待插入异步()
推荐阅读
- python - 选择用于 H2O 交叉验证的 nfolds 是否会改变使用的数据百分比?
- arrays - 在java中的数组中查找第二个重复元素
- mysql - 关于 Kubernetes 与外部世界联网的困惑
- html - Blogger 上的错误 HTML:Erreur d'analysis XML,ligne 2269,colonne 2:文档中根元素之后的标记必须格式正确
- json - 如何使用python从嵌套的json响应中提取多个json值
- sql-server - SQL Server Service Broker 性能影响
- javascript - 我需要在 Redux 应用程序的代码中的这些地方写什么(remake pure react to redux)?
- android - 卡片视图不显示,我的 recyclerview 正在工作,但卡片视图在应用程序中不可见
- c++ - 如何在 debian:latest docker 上安装 gcc-9
- swift - 编写一个泛型函数以从 Swift 中的多个枚举中获取公共变量