首页 > 解决方案 > 上传图片但大小为 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;



        }


标签: iis.net-core.net-core-3.1

解决方案


我的代码只有在我将它们(CopyTo 和 Insert)都更改为 Async with await 时才有效

等待 CopyToAsync()

等待插入异步()


推荐阅读