Upload Photo .net c#

Upload Photo .net c#

tm3610tm3610 Posts: 16Questions: 2Answers: 0
edited July 2015 in Plug-ins

hii I purchased the data table editor and i add the upload.js plug-in but i don't know how אם Implement server side plug-in upload please can someone help me with that?? i need to upload to image only to file system mot to db tnx you all....

client side js

                  {label: "Info:",name: "info", type: "upload", display: function ( val, row ) {
                        return val && row.image.webPath ?
                            '<img src="'+row.image.webPath+'"/>' :
                            'No image';
                    }}

server side not mvc

                Field res;
                res = new Field("info").Upload(new        Upload(context.Request.PhysicalApplicationPath + @"uploads\__ID____EXTN__"));
             context.Response.Clear();
             context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(res));

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited July 2015

    Hi,

    Thanks for your purchase. Just for reference, I've deleted your three other discussions on this topic - just one post is enough :-)

    The method you have used:

    res = new Field("info").Upload(new
    Upload(context.Request.PhysicalApplicationPath + @uploads__ID____EXTN__));

    Is basically correct - however, there is no __ID__ since you are not storing information on the database. You could use __NAME__ instead. You obviously need to be careful with this since uploading files with the same name could cause an issue. This is one of the reasons why it is a good idea to store information about the file on a database.

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0

    allan
    thank ou for the fast replay
    as you mentioned the issue about the same file name
    i realy need to store the information about the file in the db
    so how can i add the info about the file in the db (.net style not mvc) and return the info to the editor
    tnx

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    The documentation for that is available here. It is fairly generic C# code used there - not MVC specific (I primarily use WebAPI myself).

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0
    edited July 2015
    new Field("info")
           .Upload(
               new Upload((file, id) =>
               {
                   file.SaveAs(context.Request.PhysicalApplicationPath + @"uploads\" + id);
               })
                   .Db("image", "id", new Dictionary<string, object>
                {
                    {"fileName", Upload.DbType.FileName},
                    {"fileSize", Upload.DbType.FileSize}
                })
           );
    

    when i try to add this code i get 2 err

    1) Not all code paths return a value in lambda expression of type System.Func<System.Web.HttpPostedFile,dynamic,dynamic>

    2) Cannot convert lambda expression to type 'string' because it is not a delegate type

    i have the red err syntax in new Upload( (err is her)

    how can i resolved this

    tnx

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    Apologies, that was an error in the documentation. The lambda should have return id; as it needs to return a value.

    Alternatively, just use:

    new Upload( context.Request.PhysicalApplicationPath + @"uploads\__ID__" )
      .Db( ... )
    

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0
    edited July 2015
    new Upload( context.Request.PhysicalApplicationPath + @"uploads\__ID__" )
      .Db( ... )
    

    hii allan
    when i try to use this nothing append no row was insert and no err was receive.
    its just set the parameter and that it
    what do i do worng??

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Do you have an image database table with columns id, fileName and fileSize?

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0

    yes i have

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    So when the form is displayed and you click on the "upload" button - select a file to upload - then what happens? Nothing?

    If you look in your browser's debugging tools under the "Network" panel you will see the Ajax request being made - what is the reply from the server? There should be JSON data there, although it is possible that there is an error message.

    Thanks,
    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0
    edited August 2015

    ok
    when i upload the image the request go to the generic handler and ther is no err
    i can see the request and all the details

    after it i call to the function

    new Field("info")
           .Upload(
               new Upload((file, id) =>
               {
                   file.SaveAs(context.Request.PhysicalApplicationPath + @"uploads\" + id);
               })
                   .Db("image", "id", new Dictionary<string, object>
                {
                    {"fileName", Upload.DbType.FileName},
                    {"fileSize", Upload.DbType.FileSize}
                })
           );
    

    and Nothing happend the is no call to commit the upload to the db
    and the function end with no err

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    I'm sorry for the trouble you are having with this! The above code needs return id to be inserted - did you try adding that in? It shouldn't compile with out it.

    I've just tried setting this up myself and discovered that in 1.4.2 the C# version needs a join table that references the uploaded files - it throws an SQL error if that condition is not met.

    This is the code I used:

    /*
     * Controller for DB table users
     * Created by http://editor.datatables.net/generator
     */
    using System;
    using System.Collections.Generic;
    using System.Net.Http.Formatting;
    using System.Web;
    using System.Web.Http;
    using DataTables;
    using EditorGenerator.Models;
    
    namespace EditorGenerator.Controllers
    {
        public class UsersController : ApiController
        {
            [Route("api/users")]
            [HttpGet]
            [HttpPost]
            public IHttpActionResult Users()
            {
                var request = HttpContext.Current.Request;
                var settings = Properties.Settings.Default;
    
                using (var db = new Database(settings.DbType, settings.DbConnection))
                {
                    var response = new Editor(db, "users", "id")
                        .Field(
                            new Field("users.image")
                               .Upload(
                                   new Upload((file, id) =>
                                   {
                                       file.SaveAs(request.PhysicalApplicationPath + @"uploads\" + id);
                                       return id;
                                   })
                                       .Db("files", "id", new Dictionary<string, object>
                                    {
                                        {"filename", Upload.DbType.FileName},
                                        {"filesize", Upload.DbType.FileSize}
                                    })
                                )
                        )
                        .Field(new Field("files.filename"))
                        .LeftJoin("files", "users.image", "=", "files.id")
                        .Model<UsersModel>()
                        .Process(request)
                        .Data();
    
                    return Json(response);
                }
            }
        }
    }
    

    Very similar to your own, although I've customised the database fields for my use case and added the LeftJoin.

    The good news is that the Upload is fully integrated into Editor 1.5 (no need for the Javascript plug-in) and this issue is not present in 1.5 as I've redesigned the upload to be easier to use!

    1.5 will be out either at the end of this week or the start of next :-)

    Regards,
    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0

    thank you for the replay
    i will try the last solution you send me ,and if i will have any err
    i will wait to the last version next week
    thank you

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0

    hii allan

    is there any way to upload a photo without using your .NET class??
    because when i get to the server side i have a stored procedure that insert all the data all in one.. so is there any way to use you javascript client side and my stored procedure server side to upload an image??
    tnx

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes, but you would need to handle the request yourself. Unfortunately, at the moment the client / server documentation hasn't had the upload information added to it (its on my to do list!) but you could use the browser's network tools to look at the examples on the Editor site to review the parameters sent to the server. There are three:

    • action which is == upload
    • uploadField - the field name
    • upload - The file

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0
    edited August 2015

    action which is == upload
    •- uploadField - the field name
    •- upload - I don't get the filed name whay???

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I don't get the filed name whay???

    Do you mean "what" or "why"? I don't really understand myself I'm afraid. The field name is the fields.name value you assign to the upload field. So you know which field is uploading a file if you happen to have multiple upload options.

    Allan

  • tm3610tm3610 Posts: 16Questions: 2Answers: 0

    sorry for the misunderstood....
    what I meant is I don't get the file name when i'm in the Handler
    so the upload field is empty

    the rest of the fields are ok
    action = upload
    uploadField = field name
    but upload is empty

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'm afraid I still don't quite understand. The file name is not submitted as part of the three parameters Editor sends, just the three I listed above. You should be able to read the file name from the meta data about the file, which will depend upon how your server-side environment handles uploaded files.

    Allan

This discussion has been closed.