如何使用Switch Case编写两个处理程序

时间:2016-04-29 05:39:30

标签: asp.net

我写了两个处理程序,但我现在只需要编写一个使用switch case的处理程序如何写我试过但不工作。请帮我。这是我的两个处理程序代码,我希望在switch case.please解决这个问题

public void ProcessRequest(HttpContext context)
{
    string fname = string.Empty;
    string ffname = string.Empty;
    string m_Result = string.Empty;
    DataTable dt = new DataTable();
    Guid id = Guid.NewGuid();

    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
            {
                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                fname = testfiles[testfiles.Length - 1];
            }
            else
            {
                fname = file.FileName;
                ffname = "~/Adds_uploads/" + fname;

            }
            string p = Path.GetFileNameWithoutExtension(fname);
            fname = Path.Combine(context.Server.MapPath("~/Adds_uploads/"), p);
            file.SaveAs(fname + id + Path.GetExtension(ffname));
            string dirName1 = new DirectoryInfo(fname).Name;

            FileInfo fInfo = new FileInfo(ffname);
            String dirName = fInfo.Directory.Name + '/' + dirName1 + id + Path.GetExtension(ffname);

            HttpContext.Current.Response.Write(JsonConvert.SerializeObject(dirName));
            //RAID = context.Request.QueryString["RA_ID"].ToString();
            //UploadFileToDB(file, RAID);
        }
    }
}

public bool IsReusable {
    get {
        return false;
    }
}

第二位处理程序

public void ProcessRequest(HttpContext context)
{
    string fname = string.Empty;
    string ffname = string.Empty;
    string m_Result = string.Empty;
    DataTable dt = new DataTable();
    Guid id = Guid.NewGuid();

    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
            {
                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                fname = testfiles[testfiles.Length - 1];
            }
            else
            {
                fname = file.FileName;
                ffname = "~/Fileuploads/" + fname;

            }
            string p = Path.GetFileNameWithoutExtension(fname);
            fname = Path.Combine(context.Server.MapPath("~/Fileuploads/"), p);
            file.SaveAs(fname + id + Path.GetExtension(ffname));
            string dirName1 = new DirectoryInfo(fname).Name;

            FileInfo fInfo = new FileInfo(ffname);
            String dirName = fInfo.Directory.Name + '/' + dirName1 + id + Path.GetExtension(ffname);

            HttpContext.Current.Response.Write(JsonConvert.SerializeObject(dirName));
            //RAID = context.Request.QueryString["RA_ID"].ToString();
            //UploadFileToDB(file, RAID);
        }
    }
}

public bool IsReusable {
    get {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为上传路径的唯一区别。我建议你从视图传递另一个输入,然后根据它确定上传路径。

即 你会有

//for case one
<input type='hidden' name='uploadType' value='files' />
//for case two
<input type='hidden' name='uploadType' value='adds' />

在您的个人文件上传表单中,然后在您的操作方法中,您可以使用

var mappedDirectory = HttpContext.Current.Request.Form["uploadType"].ToString() == "adds" ? "~/Adds_uploads/" : "~/Fileuploads/";

并在fname构造中使用此字符串。所以你的新方法就像

public void ProcessRequest(HttpContext context)
{
    var mappedDirectory = HttpContext.Current.Request.Form["uploadType"].ToString() == "adds" ? "~/Adds_uploads/" : "~/Fileuploads/";
    string fname = string.Empty;
    string ffname = string.Empty;
    string m_Result = string.Empty;
    DataTable dt = new DataTable();
    Guid id = Guid.NewGuid();

    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
            {
                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                fname = testfiles[testfiles.Length - 1];
            }
            else
            {
                fname = file.FileName;
                ffname = mappedDirectory + fname;

            }
            string p = Path.GetFileNameWithoutExtension(fname);
            fname = Path.Combine(context.Server.MapPath(mappedDirectory), p);
            file.SaveAs(fname + id + Path.GetExtension(ffname));
            string dirName1 = new DirectoryInfo(fname).Name;

            FileInfo fInfo = new FileInfo(ffname);
            String dirName = fInfo.Directory.Name + '/' + dirName1 + id + Path.GetExtension(ffname);

            HttpContext.Current.Response.Write(JsonConvert.SerializeObject(dirName));
            //RAID = context.Request.QueryString["RA_ID"].ToString();
            //UploadFileToDB(file, RAID);
        }
    }
}