Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 14-Sep-11 3:03am
v3

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:

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


C#
//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 :)
 
Share this answer
 
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
C#
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;
                }
            }
        }
 
Share this answer
 
v2
Comments
fjdiewornncalwe 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)



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