비슷한 게시물이 있지만 내 상황을 나타내지 않습니다. 이것이 재발행 일 경우 사전에 사과하십시오. 내가 파일을 업로드하려면 Microsoft 웹 도우미에서 파일 업로드 도우미를 사용하고ASP.NET MVC3 동일한 페이지에서 여러 양식 보내기
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, FormCollection collection)
{
int selectedPartner, count =0;
//int selectedPartner = int.Parse(collection["PartnerID"]);
if(!int.TryParse(collection["PartnerID"], out selectedPartner))
{
selectedPartner = 0;
ModelState.AddModelError("", "You must pick a publishing agency");
}
IList<Partner> p = r.ListPartners();
ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", selectedPartner);
//make sure files were selected for upload
if (fileUpload != null)
{
for (int i = 0; i < fileUpload.Count(); i++)
{
//make sure every file selected != null
if (fileUpload.ElementAt(i) != null)
{
count++;
var file = fileUpload.ElementAt(i);
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
// need to modify this for saving the files to the server
var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName);
file.SaveAs(path);
}
}
}
}
if (count == 0)
{
ModelState.AddModelError("", "You must upload at least one file!");
}
return View();
}
@FileUpload.GetHtml(initialNumberOfFiles:1,allowMoreFilesToBeAdded:true,includeFormTag:true, uploadText: "Upload")
@model IEnumerable<EpubsLibrary.Models.Partner>
@{ using (Html.BeginForm("Index","Epub"))
{
@Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None")
<input type="submit" value="send" id="pickPartner" hidden="hidden"/>
}
}
<script type="text/javascript">
$(".file-upload-buttons input").click(function() {
$("#pickPartner").click();
});
</script>
내 컨트롤러이기 때문에
나는 나의 볼 수 있습니다. 내가 겪고있는 문제는 도우미가 양식을 작성했고 동일한 페이지에서 자료를 제출해야하는 또 다른 양식이 있습니다.
제출 버튼을 링크하여 업로드를 클릭하면 다른 양식 데이터도 전송되었지만 데이터가 전송되지 않도록 할 수 있다고 생각했습니다. 각 양식은 별다른 문제없이 독립적으로 작동하지만 함께 작업해야합니다. 어떤 조언을 주시면 감사하겠습니다. 내가
@model IEnumerable<EpubsLibrary.Models.Partner>
@{ using (Html.BeginForm("Index","Epub"))
{
@Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None")
@FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload")
<input type="submit" value="send" id="pickPartner"/>
}
}
와보기를 업데이트하지만 지금은 파일 데이터가 더 이상 전달 받고하지 않는 것 좋아
.
- 업데이트 -
다음과 같이 변경했습니다. 뷰는 이제
@model IEnumerable<EpubsLibrary.Models.Partner>
@{ using (Html.BeginForm("Index", "Epub", new { enctype = "multipart/form-data" }))
{
@Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None")
@FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload")
<input type="submit" value="send" id="pickPartner"/>
}
}
내가 (이 경우) 파일 데이터가 게시물에 보냈지고하는 방법을 알아 내려고 노력하고 있어요
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, int PartnerID = 0)
//public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, FormCollection collection)
{
int count =0;
IList<Partner> p = r.ListPartners();
ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", PartnerID);
//make sure files were selected for upload
if (fileUpload != null)
{
for (int i = 0; i < fileUpload.Count(); i++)
{
//make sure every file selected != null
if (fileUpload.ElementAt(i) != null)
{
count++;
var file = fileUpload.ElementAt(i);
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
// need to modify this for saving the files to the server
var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName);
file.SaveAs(path);
}
}
}
}
if (count == 0)
{
ModelState.AddModelError("", "You must upload at least one file!");
}
return View();
}
}
컨트롤러처럼 보이는 그래서 저장할 수 있습니다 파일.
- 대답과 함께 최종 업데이트 -
그런데 문제는 내가 발견 한 또 다른 문제는 I가 필요했다 includeFormTag: false
을 설정 @FileUpload
하고 필요로 두 배 .. 첫번째 문제로 판명 확인해주세요 @Html.BeginForm
FormMethod.Post
fileUpload 카운트가 0으로 되돌아 왔을 때 발견되었습니다. 프로파일 러를 실행하여 파일 데이터가 실제로 게시되지 않았다고 지적했습니다. 다음은 수정 된 코드입니다.
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload, int PartnerID = 0)
{
int count =0;
IList<Partner> p = r.ListPartners();
ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name", PartnerID);
//make sure files were selected for upload
if (fileUpload != null)
{
for (int i = 0; i < fileUpload.Count(); i++)
{
//make sure every file selected != null
if (fileUpload.ElementAt(i) != null)
{
count++;
var file = fileUpload.ElementAt(i);
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
// need to modify this for saving the files to the server
var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName);
file.SaveAs(path);
}
}
}
}
if (count == 0)
{
ModelState.AddModelError("", "You must upload at least one file!");
}
return View();
}
내보기
@model IEnumerable<EpubsLibrary.Models.Partner>
@{ using (Html.BeginForm("Index", "Epub", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners, "None")
@FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: true, includeFormTag: false, uploadText: "Upload")
<input type="submit" value="send" id="pickPartner"/>
}
}
내 컨트롤러는이와 함께 도움을 당신에게 @Jay 및 @Vasile Bujac 감사드립니다.
위의 편집을 참조하십시오. 파일 데이터는 이제 내가 변경 한 것과 같은 방식으로 더 이상 전달되지 않는 것처럼 보입니다. 고맙습니다. – samack
이것은 FormCollection을 보내지 않고 partnerID를 올바르게 설정하지만 여러 파일을 선택해도 fileUpload.Count()가 0 인 경우입니다. – samack
Vasile 제안과 함께 Fiddler를 실행하고 파일에 어떤 값이 게시되는지 확인하십시오. 이전에 파일이 작동했다면 계속 작업해야한다고 생각합니다. – Jay