Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code to upload file in cloud service (azure).

But its not getting into if condition. Its directly executing catch block.. i.e "ERROR OCCURRED WHILE UPLOAD FILE"

Please help me.



C#
public partial class Fileupload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
                gvbind();
        }
        filestore fs = new filestore();
        private static string parseUsingPDFBox(string filename)
        {
            org.apache.pdfbox.pdmodel.PDDocument doc = PDDocument.load(filename);
            PDFTextStripper stripper = new PDFTextStripper();
            string text = stripper.getText(doc);
            doc.close();
            return text;
        }
        string filecontent;
        string encrypt;
        string encrypt1;
        string encrypt2;
        string encrypt3;
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                     string _fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                     if (_fileExt.ToLower() == ".docx" || _fileExt.ToLower() == ".txt" || _fileExt.ToLower() == ".doc" || _fileExt.ToLower() == ".pdf")
                     {
                         string Index = string.Empty;
                         try
                         {
                             SetContainerAndPermissions();
                         }
                         catch
                         {

                         }
                         // Retrieve storage account from connection-string
                         CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("BlobConn"));

                         // Create the blob client
                         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                         // Retrieve reference to a previously created container
                         CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

                         Index = Guid.NewGuid().ToString();
                         string uploadedDocName = "Document_" + Index + "#" + FileUpload1.PostedFile.FileName;

                         CloudBlockBlob blob = container.GetBlockBlobReference(uploadedDocName);
                         blob.Metadata["FileType"] = FileUpload1.PostedFile.ContentType;
                         blob.Properties.ContentType = FileUpload1.PostedFile.FileName;
                         blob.UploadFromStream(FileUpload1.FileContent);

                         Connection ins = new Connection();
                         int result = ins.insert(FileUpload1.PostedFile.FileName.ToString(), blob.Uri.ToString(), FileUpload1.PostedFile.ContentLength.ToString() + " bytes", FileUpload1.PostedFile.ContentType.ToString(), Index, DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString(), "UnBlocked",Session["username"].ToString());
                         string filename = Path.GetFileName(FileUpload1.FileName);
                         FileUpload1.SaveAs(Server.MapPath("~/") + filename);
                         string strFileName = System.IO.Path.GetFullPath(Server.MapPath("~/" + filename));
                         string e1 = Path.GetExtension(strFileName);
                         if (e1.ToLower() == ".pdf")
                         {
                             string file = parseUsingPDFBox(strFileName);
                             file = file.Replace("'", "''");
                             byte[] mybyte = System.Text.Encoding.UTF8.GetBytes(file);
                             string returntext = System.Convert.ToBase64String(mybyte);
                          //   returntext = returntext.Trim('\'');
                              
                             encrypt = returntext;
                             byte[] mybyte1 = System.Convert.FromBase64String(returntext);
                             string returntext1 = System.Text.Encoding.UTF8.GetString(mybyte);
                             filecontent = returntext1;
                             int len = returntext.Length;
                             int split = len / 3;
                             string[] word = new string[3];
                             int j = 0;
                             int i;
                             int h;
                             int g;
                             word[0] += returntext.Substring(0, split);
                             word[1] += returntext.Substring(split, split);
                             word[2] += returntext.Substring(split + split, split);

                             encrypt1 = word[0];
                             encrypt2 = word[1];
                             encrypt3 = word[2];
                         }
                         
                         
                         string scret = GetRandomString();
                         fs.upload(FileUpload1.PostedFile.FileName.ToString(), blob.Uri.ToString(), filecontent, encrypt, encrypt1, encrypt2, encrypt3, scret,Session["username"].ToString());
                         Label1.Text = "Thank you! Your File Upload Successfully.";
                         Label1.ForeColor = System.Drawing.Color.Black;
                         //string filename = Path.GetFileName(FileUpload1.FileName);
                         //FileUpload1.SaveAs(Server.MapPath("~/") + filename);
                         //string strFileName = System.IO.Path.GetFullPath(Server.MapPath("~/"+filename));
                         //   File.Encrypt(strFileName);
                         // btnEncryp(strFileName);
                         //  EncryptFile(strFileName, "c:\\myfileEncrypted.rar", "1234512345678976");
                         gvbind();
                     }
                     else
                     {
                         Label1.Text = "Please Select Valid Files to Upload";
                     }
                }
                else
                {
                    Label1.Text = "Please Select any Files to Upload";
                    Label1.ForeColor = System.Drawing.Color.Red;
                }

            }
            catch
            {
                //throw new Exception("Error"+ex.ToString());
                Label1.Text = "Sorry! Error occured while upload your Files.";
                Label1.ForeColor = System.Drawing.Color.Red;
            }
        }




But, this same code previously woking in VS 2010.



But now its not working in VS 2013.



WHy?



Help me?
Posted
Updated 11-Mar-14 5:57am
v3

1 solution

We can't.
Why not? Because we can;t run your code under the same conditions as you do. So, the first thing to do it to gather information: and that means changing your error handling to report slightly more than "Sorry" It failed".

Change the catch to grab the exception, and either log or display the exception message, stack trace and inner exception (if any).

Without that, you are just guessing, and it could be absolutely anything...
 
Share this answer
 
Comments
Aravin.it 11-Mar-14 12:08pm    
Dear Sir,

I know & I understand your concern.

I'm trying it for 2 day.. I can;t find the exact issue, that why I asked in forum.

Thank You :)
OriginalGriff 11-Mar-14 13:00pm    
So look at what the exception is trying to report. We can't - and it's information that should tell you (or us) what is causing it.
Without that, we are as blind as you - we can't see anything...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900