如何编写代码以在C#中上传Word文档 [英] How to write coding to upload a word document in c#
本文介绍了如何编写代码以在C#中上传Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨...我已经获得了此链接,请帮助我获取代码以上载c#版本1.1中的word文档.
Hi...already i got this link,plz help me to get code to upload a word document in c# version 1.1
推荐答案
查看此处:
http://msdn.microsoft.com/en-us/library/aa479405.aspx [ ^ ]
它可以用于上传许多其他类型的文档,无论其他文档还是其他文档.
或:
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 :)
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;
}
}
}
这篇关于如何编写代码以在C#中上传Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文