Introduction design basically consists of a text area for entering the description abt the file and html input = file ,(fileupload control) in the description field we need to enter the description about the file and in the html input control we need browse for a file which u want to upload
Paragraph Heading 1
private void cmdSubmit_ServerClick(object sender, EventArgs e) { if(this.File1.PostedFile != null) { HttpPostedFile filePosted = this.File1.PostedFile; string tempfileName = filePosted.FileName; if(tempfileName == "") { //Response.Write(""); return; } tempfileName = tempfileName.Substring(tempfileName.LastIndexOf("\\")+1); int version = CheckForFileNameInDb(tempfileName); if(version > 0 && version.ToString() != null) version += 1; else version = 1; int pos = tempfileName.IndexOf("."); string temp = "[" + version + "]"; string fileName = tempfileName.Insert(pos,temp); string localPath = System.Configuration.ConfigurationSettings.AppSettings["articleFolder"].ToString(); string fullPath = localPath + "/" + fileName; string upLoadedBy = this.cmbUploadedBy.SelectedItem.Text; string fileDesc = this.txtDesc.Value; filePosted.SaveAs(Server.MapPath(fullPath)); try { if(lconn.State == ConnectionState.Open) lconn.Close(); lconn.Open(); string cmdText = String.Format("Insert into dms_doc_details(dms_docname,dms_docdesc,dms_docuploadedby,dms_docversion) values('{0}','{1}','{2}','{3}')",tempfileName,fileDesc,upLoadedBy,version); cmd = new MySqlCommand(cmdText,lconn); cmd.ExecuteNonQuery(); //Response.Write(""); //Response.Write(""); // Response.Redirect("ListDocuments.aspx"); } catch(MySqlException ex) { Response.Write(ex.Message); } catch(Exception ex) { Response.Write(ex.Message); } finally { lconn.Close(); cmd.Dispose(); } } ClearControls();
Paragraph Heading N Using the above code the file will be uploaded to the specified path that has been mentioned in the web.config and description , filenames will be stored in the database , if the user was uploading the same documnet the version of the document will be increased by one
< ItemTemplate > < a > href="FileData.aspx?did=<%# DataBinder.Eval(Container.DataItem,"dms_docid")%>"><%# DataBinder.Eval(Container.DataItem,"dms_docname")>%> </a> < /ItemTemplate > above code is the asp code for providing the hyperlink to the document name in the datagrid when we click on the link the document has to be opened or saved.
code for opening the document
string path = MapPath(fullPath); string fileName = Path.GetFileName(path); string extension = Path.GetExtension(path); string type = ""; if(extension != "") { switch(extension.ToLower()) { case ".htm": case ".html": type = "text/HTML"; break; case ".txt": type = "text/plain"; break; case ".doc": case ".rtf": type = "Application/msword"; break; } } int pos = fileName.IndexOf(".")- fileName.IndexOf("["); fileName = fileName.Remove(fileName.IndexOf("["),pos); if(forcedownload) Response.AppendHeader("content-disposition","attachment; filename=" + fileName); if(type != "") Response.ContentType = type; Response.WriteFile(path); Response.End();
Summary
|
No responses found. Be the first to respond and make money from revenue sharing program.
|