Home > front end >  Ask: the form data upload - use FormData, MultipartFile!
Ask: the form data upload - use FormData, MultipartFile!

Time:09-23

This is my first form:
 
& lt; The form method="post" id="addproductform" enctype="multipart/form - data" & gt;
& lt; H1 & gt; Add the goods & lt;/h1 & gt;
Name of commodity: & lt; Input type="text" id="goodsname"/& gt;
& lt; br/>
Commodity images: & lt; Input type="file" id="goodsphoto"/& gt;
& lt; br/>
& lt; br/> & lt; br/>
Product category & lt; br/>
Level 1 categories:
& lt; Select id="catalogue_select1" & gt;
& lt;/select>
Level 2 categories:
& lt; Select id="catalogue_select2" & gt;
& lt;/select>
Level 3 categories:
& lt; Select id="catalogue_select3" & gt;
& lt; Option> Level 3 electrical & lt;/option>
& lt; Option> Level 3 ladies & lt;/option>
& lt; Option> Level 3 men's & lt;/option>
& lt;/select>
& lt; br/> & lt; br/>
, commodity specifications set & lt; br/> & lt; br/>
& lt; Div id="goodsItemDiv" & gt;
& lt; Span & gt; Memory & lt;/span> :
& lt; Span class='unchecked' name='1' checked='false' onclick='change (this); '> 4 g & lt;/span>
& lt; Span class='unchecked' name='1' checked='false' onclick='change (this); '> 8 g & lt;/span>
& lt; Span class='unchecked' name='1' checked='false' onclick='change (this); '> 16 g & lt;/span>
& lt; Span class='unchecked' name='1' checked='false' onclick='change (this); '> The 32 gb & lt;/span>
& lt; Span class='unchecked' name='1' checked='false' onclick='change (this); '> 64 g & lt;/span>
& lt;/div>
& lt; br/>
& lt; br/>
& lt; Div>
& lt; Span & gt; Color & lt;/span> :
& lt; Span class='unchecked' name='2' checked='false' onclick='change (this); '> Black & lt;/span>
& lt; Span class='unchecked' name='2' checked='false' onclick='change (this); '> Silver & lt;/span>
& lt; Span class='unchecked' name='2' checked='false' onclick='change (this); '> Deep grey & lt;/span>
& lt;/div>
& lt; br/>
& lt; Span & gt; Commodity prices & lt;/span> : & lt; br/>
& lt; Span & gt; Inventory & lt;/span> :& lt; Div>
& lt; br/>
& lt; b> Tip: & lt;/b> & lt; Span id='resultSpan & gt; & lt;/span>
& lt; br/>
& lt; Input type="button" id="additem" onclick='additem ()' value="https://bbs.csdn.net/topics/add product specifications/& gt;" & lt; br/>
& lt; Input type="button" onclick='addThisProduct ()' value="https://bbs.csdn.net/topics/" add goods/& gt;
& lt;/div>
& lt;/form>

Main is inside a file upload: & lt; Input type="file" id="goodsphoto"/& gt;
Now I hope I will be in the form image and text messages are passed to the other in the background, but! I in the form of text information such as name of commodity, and so on, I'm not going to directly submitted to the background, but in JavaScript to fill in the data processing of good and then written to the FormData, so I didn't write directly:
 
Form. Append (" product ", $(" # addproductform ") [0]).

I am so to FormData fill data:
 
Var form=new FormData ();
& lt; ! - I want to get the form here, file upload part T ^ T I really don't know how make so write - & gt;
Form. Append (" productPhoto ", $(" # addproductform input ") [1]).
& lt; ! - productName, catalogueI, catalogueII these are after I deal with the data - & gt;
Form. Append (" productName "productName);
Form. Append (" catalogueI, "catalogueI);
Form. Append (" catalogueII, "catalogueII);
Form. Append (" catalogueIII, "catalogueIII);

Then, I am in the Controller, so receive:
 
@ ResponseBody
@ RequestMapping ("/addproduct ")
Public int addProduct (it request, @ RequestParam (" productPhoto ") MultipartFile productPhoto) throws IOException {
String productName=request. The getParameter (" productName ");
String catalogueI=request. The getParameter (" catalogueI ");
String catalogueII=request. The getParameter (" catalogueII ");
String catalogueIII=request. The getParameter (" catalogueIII ");
String originalFilePath=productPhoto. GetOriginalFilename ();
System. The out. Println (" productName is: "+ productName);
System. The out. Println (" catalogueI is: "+ catalogueI);
System. The out. Println (" catalogueII is: "+ catalogueII);
System. The out. Println (" catalogueIII is: "+ catalogueIII);
System. The out. Println (" originalFilePath is: "+ file. GetOriginalFilename ());
Return 1;
}


But was an error:
Resolved [org. Springframework. Web. Multipart. Support. MissingServletRequestPartException: Required request parts' productPhoto 'is not the present]
Say I productPhoto field does not exist? Why this happen?

In summary, my question is:
Excuse me each great god, how should I write? Main is I'm not all fill in the form of data is submitted directly, but after dealing with the JavaScript, and in a form submit pictures, at this point I front end, back end how to write? Where I have written error?

CodePudding user response:

Any answer for me once ah
Small white really won't do...

CodePudding user response:

Why use js processing, Spring is can accept together by sheSpring js

CodePudding user response:

Submit the submit directly,

CodePudding user response:

refer to the second floor street vendor reply:
why treated with js, Spring is can accept together by sheSpring js

Because I this is to add the commodity page, where you need to add multiple products specifications, I think is: on the front end will be more than the selected commodity specifications are joining together into a JSON string, for example: [{" memory ":" 2 g ", "color" : "black"}, {" memory ":" 4 g ", "color" : "dark grey"}], so put them and commodity name of the other information to be sent to the background can be handled,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related