Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

How to edit file upload information and form fields at the same time in an upload.

17.05.2018 (👁5484)

Asp.Net upload file and form information

 

How to edit file upload information and form fields at the same time in an upload.

 

When files are uploaded to an Asp.Net Core MVC application, the upload files are first received and edited.

In order to be able to process further information from the web form, one must either take additional parameters from the URL or use the formatting

 

About the URL

//*from Querysting like Notes/Edit_Upload_Images?IDNote=11&ImageNr=1

string sIDNote = Request.Query["IDNote"];

 

The actual upload files are taken over with Request.Form.Files

//< get form_files >

//IFormFile uploaded_File

var uploaded_files = Request.Form.Files;

//</ get form_files >

..

//------< @Loop: Uploaded Files >------            

foreach (var uploaded_file in uploaded_files)

{

..

}

 

I always take form information directly from the form element enumeration

* Code not embedded here in the example ..

            //< get >

            string sHTML = form["ctlEditor_Text"];

            string sTitle = form["Note.Title"];

            string sFolder = form["Note.Folder"];

            //</ get >

 

 

 

Code example, C #, asp.net Core 2 File Upload

        [HttpPost]

        [Authorize]

        [RequestSizeLimit(1000000000)]  //1000000000=1000.000.000=1GB

        public async Task<IActionResult> Edit_Upload_Images()

        {

            // -------------< Edit_Upload_Images() > -------------

            //string sFiles_uploaded = "";

            List<string> response_list_of_Filenames = new List<string>();

 

            //< get ID and SubNr >

            //*from Querysting like Notes/Edit_Upload_Images?IDNote=11&ImageNr=1

            string sIDNote = Request.Query["IDNote"];

            long IDNote = System.Convert.ToInt64(sIDNote);

            //</ get ID and SubNr >

 

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDCurrent_User = await UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--

 

            //< get_database >

            NoteModel note;

            if (IDNote == 0)

            {

                note = _dbContext.tbl_Notes.SingleOrDefault(n => n.IDUser == IDCurrent_User && n.IsDraft == true);

                if (note == null)

                {

                    note = new NoteModel();

                    note.IsDraft = true;

                    note.DtCreated = DateTime.Now;

                    note.IDUser = IDCurrent_User;

                    note.Title = "#images";

                    note.sumImages = 0;

                    note.DtEdit = DateTime.Now;

 

                    //< save >

                    await _dbContext.tbl_Notes.AddAsync(note); //IDNote: 0->-99999

                    await _dbContext.SaveChangesAsync(true);       //IDNote: -99999->16

                    //</ save >

 

                    //< new ID >

                    IDNote = note.IDNote;

                    //Console.WriteLine("new IDNote=" + note.IDNote);

                    //</ new ID >

                }

                else

                {

                    //< last draft >

                    IDNote = note.IDNote;

                    //</ last draft >

                }

            }

            else

            {

                note = _dbContext.tbl_Notes.SingleOrDefault(n => n.IDNote == IDNote);

            }

 

            if (note == null) { return Json("NoMatch for IDNote=" + IDNote); }

            //</ get_database >

 

            //< add_response >

            response_list_of_Filenames.Add("IDNote=" + IDNote);

            //</ add_response >

 

 

            //< check Owner >

            long IDOwner = note.IDUser;

            if (IDOwner != IDCurrent_User) { return Content("You are not the Owner"); }

            //</ check Owner >

 

 

 

            //< init >

            //long uploaded_size = 0;

            string path_for_Uploaded_Files = _hostingEnvironment.WebRootPath + "\\User_Files\\Notes\\Images";

            //</ init >

 

            //< get form_files >

            //IFormFile uploaded_File

            var uploaded_files = Request.Form.Files;

            //</ get form_files >

 

            //--< get last Image >--

            //*get User Summary Table

            Note_Image_Model imageData = new Note_Image_Model();

 

            //< last_Image >

            //var lastImage = _dbContext.tbl_Notes_Images.LastOrDefault(img => img.IDNote == IDNote);

            //last Image

            Note_Image_Model lastImage = await _dbContext.tbl_Notes_Images.Where(img => img.IDNote == IDNote).OrderBy(img => img.ImageNr).LastOrDefaultAsync();

            int last_ImageNr = -1;

            if (lastImage != null)

            {

                last_ImageNr = (int)lastImage.ImageNr;

            }

            //</ last_Image >

            //------< @Loop: Uploaded Files >------            

            foreach (var uploaded_file in uploaded_files)

            {

                //----< Uploaded File >----

                try

                {

                    if (uploaded_file.ContentType.IndexOf("image") == 0)   //*image/jpeg

                    {

                        //------< Is_TypeImage >------

                        string uploaded_Filename = uploaded_file.FileName;

                        int posExtension = uploaded_Filename.LastIndexOf(".");

                        string sExtension = uploaded_Filename.Substring(posExtension + 1);

                        sExtension = sExtension.ToLower();

 

 

                        if (" .jpg .png .tiff .jpeg .bmp .gif".IndexOf( "." + sExtension)>0)

                        {

                            //----< Is_Image >----

 

 

 

                            //--< New Note_Image >--

                            last_ImageNr++;

                            Note_Image_Model newImageData = new Note_Image_Model();

                            newImageData.IDNote = IDNote;

                            newImageData.dtCreate = DateTime.Now;

                            newImageData.ImageNr = last_ImageNr;

                           

                            //< save ImageData >

                            await _dbContext.tbl_Notes_Images.AddAsync(newImageData);

                            await _dbContext.SaveChangesAsync(true);

                            //</ save ImageData >

 

                            //< add_response >

                            response_list_of_Filenames.Add("Image_" + IDNote + "_" + last_ImageNr);

                            //</ add_response >

 

                            //< Filename >

                            string image_webname = "Image_" + IDNote + "_" + last_ImageNr + "." + sExtension;

                            string new_FullFilename_on_Server = path_for_Uploaded_Files + "\\" + image_webname;

                            //</ Filename >

 

 

                            //< Copy File to Target >

                            if(System.IO.File.Exists(new_FullFilename_on_Server)) { System.IO.File.Delete(new_FullFilename_on_Server); }

                            using (FileStream stream = new FileStream(new_FullFilename_on_Server, FileMode.Create))

                            {

                                await uploaded_file.CopyToAsync(stream);

                            }

                            //</ Copy File to Target >

 

                            //----< check Properties >----

                            Image originalImage = Image.FromFile(new_FullFilename_on_Server);

                            if (originalImage.PropertyIdList.Contains(0x0112))

                            {

                                int rotationValue = originalImage.GetPropertyItem(0x0112).Value[0];

                                switch (rotationValue)

                                {

                                    case 1: // landscape, do nothing

                                        break;

 

                                    case 8: // rotated 90 right

                                            // de-rotate:

                                        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);

                                        originalImage.Save(new_FullFilename_on_Server);

                                        break;

 

                                    case 3: // bottoms up

                                        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);

                                        originalImage.Save(new_FullFilename_on_Server);

                                        break;

 

                                    case 6: // rotated 90 left

                                        originalImage.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);

                                        originalImage.Save(new_FullFilename_on_Server);

                                        break;

                                }                                                               

                            }

                            originalImage.Dispose();

                            //----</ check Properties >----

 

                            if (sExtension != "jpg")

                            {

                                //----< IsNot_Jpg >----

 

                                //< convert >

                                string old_File = new_FullFilename_on_Server;

                                image_webname = "Image_" + IDNote + "_" + last_ImageNr + ".jpg";

                                new_FullFilename_on_Server = path_for_Uploaded_Files + "\\" + image_webname;

                                Image_Methods.Image_resize_Length(old_File, new_FullFilename_on_Server, 1920);

                                if (System.IO.File.Exists(old_File)) { System.IO.File.Delete(old_File); }

                                //</ convert >

                                //----</ IsNot_Jpg >----

                            }

                            else

                            {

                                Image_Methods.Image_resize_Length(new_FullFilename_on_Server, new_FullFilename_on_Server, 1920);

                            }

 

 

 

 

                            //< resize >

                            Image_Methods.Image_resize_to_Width(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_mini.jpg", 200);

                            Image_Methods.Image_resize_to_Width(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_pad.jpg", 400);

                            Image_Methods.Image_resize_Length(new_FullFilename_on_Server, path_for_Uploaded_Files + "\\Image_" + IDNote + "_" + last_ImageNr + "_blog.jpg", 800);

                            //</ resize >

 

                            //-< get Size >-

                            System.Drawing.Image image = System.Drawing.Image.FromFile(new_FullFilename_on_Server);

                            int intWidth = image.Width;

                            int intHeight = image.Height;

                            image.Dispose();

 

                            //< save size >

                            newImageData.Width = intWidth;

                            newImageData.Height = intHeight;

                            await _dbContext.SaveChangesAsync(true);

                            //</ save size >

                            //-</ get Size >-

 

                            //----</ Is_jpg >----

                        }

                        //------</ Is_TypeImage >------

                    }

                }

                catch (System.Exception ex)  //ex

                {

                    //< error >

                    //  response_list_of_Filenames.Add("Error:" + ex.Message);

                    Console.WriteLine(ex.Message);

                    //</ error >

                }

                //----</ Uploaded File >----

            }

            //------</ @Loop: Uploaded Files >------

 

            //----< Save Data >----

            //--< data >--

            note.sumImages = last_ImageNr + 1;

            note.DtEdit = DateTime.Now;

            //--</ data >--

 

            try

            {

                //todo: _dbContext.Update(note);

                await _dbContext.SaveChangesAsync(true);

            }

            catch (DbUpdateConcurrencyException)

            {

                return Content("Update Error");

            }

            //----</ Save Data >----

 

            //< response >

            return Json(response_list_of_Filenames);

            //</ response >

            // -------------</ Edit_Upload_Images() > -------------

        }