Form Post with enctype = “multipart/form-data” causing parameters to not get passed

纵饮孤独 提交于 2019-12-12 15:36:28

问题


In an ASP.NET MVC 3 (Razor) project i 'm trying to upload a picture: the relevant part of the view:

@using (@Html.BeginForm( new {enctype = "multipart/form-data" }))
{
    <text>Select a file </text>    
    <input type="file" name="file" />     
    <input type="submit" value="Upload" />        
}

stating explicitly the enctype parameter is "responsible" for stripping off the parameter part. For example, if the URL (opening the view) was the following:

mydomain/Controller/Action/id?parameter1=somevalue1

the BeginForm-statement in the form above would give (posting back) the following:

mydomain/Controller/Action/id

thus stripping off the part: ?parameter1=somevalue1 which is needed!

How do i take care of this?


回答1:


Since you're already posting data to the server I'd put the required parameter information in the form as hidden fields.

Asking the question "Can I send this collection of information to this URL without needing to populate other collections?"

With the querystring method the answer is no. I'd put it as a hidden field.

@Html.Hidden("SomeParameter", SomeValue);



回答2:


Digging a little bit deeper i discovered that i can do:

@using (@Html.BeginForm(new{parameter1= Request["parameter1"]},  new {enctype = "multipart/form-data" }))
{
    <text>Select a file </text>    
    <input type="file" name="file" />     
    <input type="submit" value="Upload" />        
}

Notice how the Html.BeginForm differs from the initial.




回答3:


You can try by calling the subit from java script by appeding the parameters to the action example document.forms[0].action="photoprocess.asp?="+

and then submit



来源:https://stackoverflow.com/questions/11106784/form-post-with-enctype-multipart-form-data-causing-parameters-to-not-get-pas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!