Specializations

Wednesday, November 21, 2012

File upload in AP.net


 Conditional base file uploading with database given path


private string Uploadfile()
        {
            string UPURL = string.Empty;
            if (fucFileUpload.HasFile)
            {
                try
                {
                    DataTable dt = dtImportCodes.Select("imp_cod_value='HLPFilePath'").CopyToDataTable();
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        string filename = Path.GetFileName(fucFileUpload.FileName);
                        string fileType = "L";
                        dtImportCodes.AsEnumerable().Where(p => p["imp_cod_value"].ToString().Trim() == "HLPSource").ToList().ForEach(delegate(DataRow dr) { fileType = dr["directorypathFiled"].ToString(); });
                        if (fileType == "L")
                        {
                            string filePath = Server.MapPath(dt.Rows[0]["directorypathFiled"].ToString()) + "\\" + filename;
                            fucFileUpload.PostedFile.SaveAs(filePath);
                        }
                        else
                        {
                            fucFileUpload.SaveAs(dt.Rows[0]["directorypathFiled"].ToString() + "\\" + filename);
                        }
                        //fucFileUpload.SaveAs(dt.Rows[0]["directorypathFiled"].ToString() + "\\" + filename);
                       UPURL = filename;

                      
                    }
                }
                catch (Exception ex)
                {
                    LogError("GTKEntityHelp.UILayer.Security.Utilities", "Uploadfile", ex.GetBaseException().Message.ToString().Replace("\r\n", ""), UserCode, true);
                }

            }
            return UPURL;
        }

No comments:

Post a Comment