How To Upload File In C#, asp.net ,MVC and Create Directory If Not Exist

How To Upload File In C#, asp.net ,MVC and Create Directory If  Not Exist








using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;

namespace myProject
{
    public static class Utils

    {


public static bool SaveFile(HttpPostedFile postedFile, string filePath, string fileName)
        {


            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            if (postedFile.ContentLength > 0)
            {
                postedFile.SaveAs(filePath + fileName);
                return true;
            }
            else
            {
                return false;
            }
        }
  }
}

How To Move Image from One location To Another Location using C#,Asp.net MVC


How To Move Image from One location To Another Location using C#




using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;

namespace myProject
{
    public static class Utils

    {

public static void MoveImages(string sessPath, string dirPath, int fileLimitToMove = 25)
        {
            try
            {
                DirectoryInfo sessionDirInfo = new DirectoryInfo(sessPath);
                if (sessionDirInfo.Exists)
                {
                    DirectoryInfo desDirInfo = new DirectoryInfo(dirPath);
                    if (!desDirInfo.Exists)
                        desDirInfo.Create();

                    int fileLimitIndex = 0;
                    foreach (var file in sessionDirInfo.GetFiles())
                    {
                        if (fileLimitIndex >= fileLimitToMove)
                        {
                            break;
                        }
                        string fileName = Path.GetFileName(file.Name);
                        if (File.Exists(Path.Combine(dirPath + "\\", fileName)))
                        {
                            File.Delete(Path.Combine(dirPath + "\\", fileName));
                        }
                        System.IO.File.Move(sessPath + "\\" + fileName, Path.Combine(dirPath + "\\", fileName));
                        fileLimitIndex = fileLimitIndex + 1;
                    }
                    Directory.Delete(sessPath, true);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
   }
}

How To Convert HttpPostedFileBase To HttpPostedFile in c#


How To Convert HttpPostedFileBase To HttpPostedFile in c#




Here We Need To Convert HttpPostedFileBase To HttpPostedFile using C# , asp.net MVC

using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

using System.Collections.Generic;

namespace myProject
{
    public static class Utils

    {
public static HttpPostedFile HttpPostedFileBaseToHttpPostedFile(HttpPostedFileBase file)
        {
            var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
            var obj = (HttpPostedFile)constructorInfo
                      .Invoke(new object[] { file.FileName, file.ContentType, file.InputStream });
            return obj;
        }
}

Featured Post

How To Dynamic Upload multiple files with extra detail of model in asp.net core

 How To Dynamic Upload multiple files with extra detail of model in asp.net core  1). firstly create Model File  namespace MyModel {     pub...

Popular Posts