c# - Uploading Image to Server from NameValueCollection -
i using asp.net 4.5 vb.net
this testpage.aspx code
<%@ page language="vb" masterpagefile="~/master.master" autoeventwireup="false" codefile="testpage.aspx.vb" inherits="testpage" %> <asp:content id="content1" contentplaceholderid="contentplaceholdermaster" runat="server"> <form role="form" method="post" id="test_form" action="testpageload.aspx" <div class="form-group"> <input class="form-control" type="text" id="headline" name="headline" /> </div> <div class="form-group"> <div class="fileupload btn btn-default"> <span>upload article image</span> <input type='file' id="imginp" name="imginp" class="upload" /> </div> </div> <button type="submit" name="submitreport" id="submitreport">submit report</button> </form> </asp:content>
this testpageload.aspx.vb code on page_load event
dim nvc namevaluecollection = request.form() lblheadline.text = nvc("headline") dim myfiletoupload string myfiletoupload = nvc("imginp") dim folderpath string dim folderpathdoc string folderpath = "~/pics/" if myfiletoupload.hasfile dim strfilename string dim extention string = system.io.path.getfilename(myfiletoupload.postedfile.filename) dim noextention string = system.io.path.getfilenamewithoutextension(myfiletoupload.postedfile.filename) strfilename = noextention + "_logo" + extention.substring(extention.lastindexof(".")) dim filepath [string] = server.mappath(folderpath & strfilename) myfiletoupload.saveas(filepath) end if
now code works fine populating or saving data on testpageload.aspx.vb, cant upload image on load page. there way around it? not want change controls asp.net controls on testpage.aspx needs stay plain html. uploading image problem.....
vb.net or c# code fine
found code , did job @ end.
<form action="testpageload.aspx" method="post" enctype="multipart/form-data"> <input type="file" name="imginp" /> </form>
page_load event on testpageload.aspx.cs
if(request.files["imginp"] != null) { httppostedfile myfile = request.files["imginp"]; //setting location upload files string targetlocation = server.mappath("~/pics/"); try { if (myfile.contentlength > 0) { //determining file name. can format wish. string filename = myfile.filename; //determining file size. int filesize = myfile.contentlength; //creating byte array corresponding file size. byte[] filebytearray = new byte[filesize]; //posted file being pushed byte array. myfile.inputstream.read(filebytearray, 0, filesize); //uploading formatted file server. myfile.saveas(targetlocation + filename); } } catch(exception bluescreen) { //handle errors } }
all to: uploading files in asp.net without using fileupload server control
Comments
Post a Comment