파일을 업로드하려고하지만 enctype = "multipart/form-data"를 사용하는 asp.net에서 응용 프로그램을 개발 중입니다. 양식 모음이 비어있을 때 나는 enctype을 사용하지 않는다. 폼 콜렉션은 파일 업로드 이름이지만 Request.Files.count = 0이다. 나는 파일 업로드와 폼 콜렉션에서 업로드 파일의 이름을 얻고 싶다. 어떤 해결책?양식 컬렉션이 비어 있습니다. 파일 업로드에 enctype을 사용하는 경우
2
A
답변
0
다음은 나를 위해 잘 작동 : 파일 입력 파일을 가져 컨트롤러 액션 이후에 사용됩니다 name
이 있어야합니다
@using (Html.BeginForm("someaction", "somecontroller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<button type="submit">Upload</button>
}
공지있다. Request.File.Count = 0을 얻고 있다는 사실은 입력 필드에 이름을 제공하지 않았 음을 나타내는 매우 강력한 표시입니다.
과 조치 :[HttpPost]
public ActionResult SomeAction(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var filename = Path.GetFileName(file.FileName);
filename = Path.Combine(Server.MapPath("~/uploads"), filename);
file.SaveAs(filename);
}
return View();
}
하고 FormCollection (나는 권장하지 것이다)를 사용하기를 원한다면 :
[HttpPost]
public ActionResult SomeAction()
{
var file = Request.Files["file"];
if (file != null && file.ContentLength > 0)
{
var filename = Path.GetFileName(file.FileName);
filename = Path.Combine(Server.MapPath("~/uploads"), filename);
file.SaveAs(filename);
}
return View();
}
또한 following blog post을 체크 아웃 할 수 있습니다. 다음 코드
1
체크 아웃 :
@using (Html.BeginForm("Create", "Employees", FormMethod.Post,new{ enctype="multipart/form-data"}))
{
@Html.TextBoxFor(model => model.Name)
@Html.TextBoxFor(model => model.Resume, new { type = "file" })
<p>
<input type="submit" value="Save" />
</p>
@Html.ValidationSummary()
}
추가 컨트롤러의 당신의 각 작업에서 다음 코드,
[HttpPost]
public ActionResult Create(EmployeeViewModel viewModel)
{
if (Request.Files.Count > 0)
{
foreach (string file in Request.Files)
{
string pathFile = string.Empty;
if (file != null)
{
string path = string.Empty;
string fileName = string.Empty;
string fullPath = string.Empty;
path = AppDomain.CurrentDomain.BaseDirectory + "directory where you want to upload file";//here give the directory where you want to save your file
if (!System.IO.Directory.Exists(path))//if path do not exit
{
System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "directory_name/");//if given directory dont exist, it creates with give directory name
}
fileName = Request.Files[file].FileName;
fullPath = Path.Combine(path, fileName);
if (!System.IO.File.Exists(fullPath))
{
if (fileName != null && fileName.Trim().Length > 0)
{
Request.Files[file].SaveAs(fullPath);
}
}
}
}
}
}
:
코드에 따라 뷰의 형태로 인코딩 유형을 추가
나는 asssumed 경로는 디렉토리의 디렉토리에있을 것입니다 ... 당신은 파일을 저장하고자하는 자신의 경로를 줄 수 있습니다
0
대해 사용하기 무엇 :
public ActionResult YourAction()
{
Request.Files[0]; //Check this out !
}
상세 정보 :
유래 : https://stackoverflow.com/a/5060318
Tallan 블로그 : http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/
다음@Microsoft.Web.Helpers.FileUpload.GetHtml("File", 1, false, false, null, null)
컨트롤러에
관련 문제
- 1. 클라이언트에 도착하면 컬렉션이 비어 있습니다.
- 2. Exchange 웹 서비스 첨부 파일 컬렉션이 비어 있습니다.
- 3. 레일 부분 컬렉션이 비어 있습니까?
- 4. 파일 업로드에 Python/CGI를 사용하는 방법
- 5. Ajax에서 작업을 호출 할 때 HttpRuntime.Cache 컬렉션이 비어 있습니다.
- 6. ListView 삽입 이벤트 - ListViewInsertEventArgs 값 컬렉션이 비어 있습니다.
- 7. 항목 childwindow를 다시 표시 할 때 아이템 컬렉션이 비어 있습니다.
- 8. enctype을 변경하지 않고 JSP로 파일 업로드
- 9. 일부 파일 용 파일의 경우 DownloadUrl이 비어 있습니다.
- 10. 양식 제출 후 $ _POST가 비어 있습니다.
- 11. 데이터베이스의 파일 업로드에 관하여
- 12. 파일 업로드에 문제가 발생했습니다.
- 13. URL에 변수가있는 경우 $ _GET이 비어 있습니다.
- 14. Android simple_list_item_single_choice 비어 있음 SimpleCursorAdapter를 사용하는 경우
- 15. JAX-WS,받은 LinkedHashMap 컬렉션이 비어 있습니까?
- 16. 소켓 전송 파일 : 내용이 비어 있습니다.
- 17. CKEditor 파일 관리자가 파일 업로드에 방해가됩니까?
- 18. 배열이 갑자기 비어 있습니다.
- 19. JSF에서 파일 업로드에 대한 기본 질문
- 20. WCF RIA 서비스 양식 인증 ServiceContext.User.Identity.Name 비어 있습니다.
- 21. DOC 및 PDF 형식의 파일 업로드에 BlobStore를 사용하는 방법
- 22. 이 파일 업로드에 문제가 있습니까?
- 23. 파일 업로드에 Ajax와 Flash를 사용하는 JavaScript 라이브러리를 찾고 있는데
- 24. WCF 오류 - Reference.cs가 비어 있습니다 (비어 있음)
- 25. HTML 폼 : Firefox에서 인코딩과 enctype을 결합하는 문제
- 26. 레이아웃 애니메이션이 비어 있습니다.
- 27. 파일이 비어 있습니다.
- 28. $ _FILE이 비어 있습니다.
- 29. ComponentOne FlexGrid가 비어 있습니다.
- 30. admin_init에서 wp_query가 비어 있습니다.