locked
JSON for image upload RRS feed

  • Question

  • User219039814 posted

    Can you tell me what wrong with my code..

    [WebMethod]
    [ScriptMethod]
    public static void InsertWebService(check c)
    {

    HttpPostedFile postedFile= c.photo;             
    string fileName = System.IO.Path.GetFileName(postedFile.FileName);

    //Set the Image File Path.
    string filePath = "~/check/" + fileName;

    //Save the Image File in Folder.
    postedFile.SaveAs(HttpContext.Current.Server.MapPath(filePath));

    //performing normal insert query ..
    Connection con = new Connection();
    SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    conn = con.getConnection();
    try
    {

    conn.Open();
    query = ""; 

    // Query written 

    }//close finally

    AJAX call..

    student.photo = $('#photo').val(),   // I need to know if i can send the path like this.. this gives c:\fakepath\fileuploaded.jpg

    //normal call to web service

    can you tell what is going wrong

    Wednesday, July 11, 2018 9:02 AM

All replies

  • User-369506445 posted

    I think you can do it with a <g class="gr_ gr_21 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="21" data-gr-id="21">webmethod</g>, you can try below link

    https://codepedia.info/upload-image-using-jquery-ajax-asp-net-c-sharp/

    Wednesday, July 11, 2018 11:55 AM
  • User219039814 posted
    My code is posted..can u tell what is it that is going wrong
    Wednesday, July 11, 2018 12:08 PM
  • User-369506445 posted

    Ok.then let's put your complete view code here

    Wednesday, July 11, 2018 2:45 PM
  • User219039814 posted
    I want to send uploaded image filename via json to web method wherein i can store the file in a specified folder..
    I am trying for past 2 days.. can u help pls
    Wednesday, July 11, 2018 3:31 PM
  • User-1171043462 posted

    student.photo = $('#photo').val(),   // I need to know if i can send the path like this.. this gives c:\fakepath\fileuploaded.jpg

    It is not possible to get Path of Client machine.

    Solution] FakePath problem in ASP.Net FileUpload control

    Reasons

    1. It is disabled in all browsers.
    2. There is no need for you to find out from where File was selected.

    Wednesday, July 11, 2018 3:38 PM
  • User219039814 posted
    If i extract only the filename.. say..
    Red.jpg.. then how does it get sent to webmethod and how does it recieve
    Wednesday, July 11, 2018 3:44 PM
  • User219039814 posted
    string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + fileName));
    Can the above code be executed in the web method??
    Becoz i did.. but i dont know how get the filename red.jpg.. and also.. i am not using asp fileupload becoz of some constrainrlt.. i have to use input type file.. so now can u pls tell me..
    Wednesday, July 11, 2018 3:56 PM
  • User-369506445 posted

    <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="11" data-gr-id="11">no </g><g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="10" data-gr-id="10"><g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="11" data-gr-id="11">,</g>you</g> only get the image name <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="7" data-gr-id="7">befor</g> send,

    In order to send file/s, <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="8" data-gr-id="8">your</g> contentType must be multipart/form-data

    but

    WebMethod or "Page methods" expect contentType: "application/json;

    for this <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="9" data-gr-id="9">reason</g> I think you can't do it

    <g class="gr_ gr_27 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="27" data-gr-id="27">also</g> refer to below link

    https://stackoverflow.com/questions/25530098/unable-to-call-server-side-webmethod-function

    Wednesday, July 11, 2018 3:56 PM
  • User219039814 posted
    Besides the file info.. i have lot other info to be passed to server.. so how can i do it
    Wednesday, July 11, 2018 4:25 PM
  • User-369506445 posted

    you can pass other info to <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="7" data-gr-id="7">server</g>, you can't only pass your Image file

    here is my before post for <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="9" data-gr-id="9">send</g> other info to <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="8" data-gr-id="8">server</g>, please follow below link

    https://forums.asp.net/t/2046792.aspx?How+to+access+a+vlue+in+webmethod+using+jquery

    Wednesday, July 11, 2018 4:44 PM
  • User-1171043462 posted

    For File Upload you will need to use a Generic handler.

    http://www.binaryintellect.net/articles/f2a2f1ee-e18a-416b-893e-883c800f83f4.aspx

    And Upload of File and Saving other information you will need to keep separate.

    Wednesday, July 11, 2018 7:39 PM