asp.net mvc - Asp Mvc Xml Upload -
i uploading xml , posting on action method.at top of received file there information ------webkitformboundarytuarn4bf71aoefqg content-disposition: form-data; name="file"; filename="samplexmp.xml" content-type: text/xml because of cannot load on xdocument.this code
public actionresult postxml() { string xml = ""; if (request.inputstream != null) { streamreader stream = new streamreader(request.inputstream); string x = stream.readtoend(); xml = httputility.urldecode(x); var xmldocument = xdocument.parse(xml);//exception (invalid data @ root) } return view(); }
this view
@using (html.beginform("postxml", "home", formmethod.post, new { enctype = "multipart/form-data" })) { input type="file" name="file" input type="submit" value="upload" }
how can remove additional data?
you don't want read whole stream because multipart , going have part boundaries written whole stream.
the correct way of reading uploaded file in mvc
[httppost] public actionresult upload(httppostedfilebase file) { try { if (file.contentlength > 0) { var filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/app_data/images"), filename); file.saveas(path); } viewbag.message = "upload successful"; return redirecttoaction("index"); } catch { viewbag.message = "upload failed"; return redirecttoaction("uploads"); } }
from article http://rachelappel.com/2015/04/02/upload-and-download-files-using-asp-net-mvc/
Comments
Post a Comment