Click here to Skip to main content
12,955,372 members (60,553 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi...already i got this link,plz help me to get code to upload a word document in c# version 1.1
Posted 13-Sep-11 3:18am
Updated 14-Sep-11 3:03am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Look here:
http://msdn.microsoft.com/en-us/library/aa479405.aspx[^]

It works for uploading many other types of documents be it woird, excel pr whatever else.

Or:

<input id="myFile"  runat="server" type="file" />


//in your code behind
if ( myFile.Value != null && myFile.Value != ""  )
{
   HttpPostedFile file = myFile.PostedFile;
   String contentType = file.ContentType;
   Decimal fileSize = ( file.InputStream.Length / 1024 );
   String fileType = Path.GetExtension( file.FileName );
   if( fileType.Equals( "doc" ) || fileType.Equals( "docx" ) )
   {
       //Go ahead and proccess client request
       String filePath = Server.MapPath( "WordDocuments") + "/" + file.FileName;
       //save this to your database
      //WordDocuments in this case is the directory in your project root where you save your uploads
   }
   else
   {
       //alert user to select right file format
   }
}
else
{
    //alert user to select file first
}


Hope that helps :)
  Permalink  
v4
Comments
Mokshagna 14-Sep-11 1:42am
   
Please,I want more clarified answer
Morgs Morgan 14-Sep-11 13:24pm
   
See my example in my solution above and let me know if that's what you want?
Mokshagna 15-Sep-11 3:56am
   
Thank u so...much Morgan!!!!....Its working fine :)
Morgs Morgan 15-Sep-11 3:58am
   
Cool beans :)
Mokshagna 15-Sep-11 6:12am
   
Hi...small doubt,here we need to save the path in database,how to save? its not saving
Morgs Morgan 15-Sep-11 7:51am
   
Please see my solution above for changes.
Also please try to make an effort to google a little, this is a very simple task. Remember learning the "hard way" sticks in your head till you drop dead and getting "full code" like this will only help you once...
Mokshagna 15-Sep-11 8:20am
   
ya...i understood,thanx for your sugggestion,now my application is running gud:)
Morgs Morgan 15-Sep-11 8:47am
   
Great!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

using System.Io;
 

 private void saveFileToFolder()
        {
            try
            {
                string filepath = "";
                HttpPostedFile file = fileUploadTasks.PostedFile;
                FileEntity objFileEntity = new FileEntity();
 
                if (hiddenFolderPath.Value.ToString() != "")
                {
                    string fileExt = Path.GetExtension(file.FileName).ToLower();
                    int filelength = file.ContentLength;
                    string fileName = Path.GetFileName(file.FileName);
                    FileEntity objFile = objDocumentBusinessFacade.CheckExistingFileNameByFolderID(Convert.ToInt32(hdnFolderSelectionCount.Value), fileName);
                    if (objFile.FileID == 0)
                    {
                        if (fileName != string.Empty)
                        {
                            if (filelength < 1048576)//1MB
                            {
                                if (fileExt == ".txt" || fileExt == ".rtf" || fileExt == ".doc" || fileExt == ".docx" || fileExt == ".xls" || fileExt == ".xlsx" || fileExt == ".pdf" || fileExt == ".htm" || fileExt == ".html" || fileExt == ".ppt" || fileExt == ".pptx")
                                {
                                    filepath = hiddenFolderPath.Value.ToString() + "/" + fileName;
                                    file.SaveAs(Server.MapPath(hiddenFolderPath.Value.ToString() + "/") + fileName);
                                    objFileEntity.FilePath = filepath;
                                    objFileEntity.FileName = fileName;
                                    objFileEntity.FolderID = int.Parse(hiddenFolderID.Value);
                                    objDocumentBusinessFacade.InsertIntoFileandDocuments(objFileEntity);
                                }
                                else
                                {
                                    lblErrorMessage.Text = "Invalid file format!";
                                }
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertIndex", "alert('File size cannot exceed 1MB(1048576 bytes) ');", true);
                                return;
                            }
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertIndex", "alert('Given file name already exists in this folder');", true);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                bool blnThrow = ExceptionPolicy.HandleException(ex, "WebUI");
                if (blnThrow)
                {
                    throw;
                }
            }
        }
  Permalink  
v2
Comments
Marcus Kramer 14-Sep-11 9:36am
   
Please wrap code in "pre" blocks. I have added them to your post here to show you how it is done. Cheers.
Mokshagna 15-Sep-11 8:30am
   
thanq:)

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,559
CHill60 3,550
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 15 Sep 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100