第一次是博客,還有丟丟小興奮呢。作為一個資深菜鳥,為了給自己留下點什么,開始記錄一些技術問題。當然也是學習過程。
下面是成品的在.net web下的應用,還有很多不足的地方,期待大家的點評。
$(document).ready(function() { $("#uploadify").uploadify({ 'buttonText':'選擇文件', 'swf': '../scripts/jquery.uploadify-v3.2/uploadify.swf?ver=<%=DateTime.Now.Ticks %>', 'uploader': 'UploadHandler.aspx',//上傳后對文件的處理頁 'auto': false,//是否自動上傳 'multi': true,//是否可以上傳多個文件 'fileSizeLimit' : 204800, 'method' : "POST", 'formData' : {'fileID' : <%="'"+Request["file_id"] + "'"%>}, 'onSelect': function(fileObj) { if(fileObj.name.indexOf('%') < 0) {//這是我自己應用的一個方法,在數據庫里判斷是否有重復文件。 AjaxSendPost('IsFileExist.aspx',<%= "'file_id=" + Request["file_id"] + "'"%> + '&file_name=' + fileObj.name + '&file_size='+fileObj.size,IsFileExist); } else { $('#uploadify').uploadify('cancel', '*'); alert("文件名中含有非法字符(%)"); } }, 'onUploadSuccess':function(file,data,response){//處理上傳成功后的事件 }, 'onCancel':function(){//取消上傳或者點擊右上角X的方法 } }); function IsFileExist(){ if(ajaxHttPRequest.readyState==4&&ajaxHttpRequest.status==200){ if(ajaxHttpRequest.responseText == "2") { alert("文件已經存在,按上傳將覆蓋已有文件!"); } else if (ajaxHttpRequest.responseText == "1") { $('#uploadify').uploadify('cancel', '*'); alert("文件已經存在!"); } } } });
cs文件處理:(UploadHandler.aspx.cs)
protected void Page_Load(object sender, EventArgs e) {
HttpPostedFile PostedFile = Request.Files["Filedata"];
string VirtualDIR = ConfigurationManager.AppSettings["VirtualDIR"];
string uploadPath = HttpContext.Current.Server.MapPath(VirtualDIR);
string AttchementuploadPath = HttpContext.Current.Server.MapPath(VirtualDIR + "/Attchements/");
string MemouploadPath = HttpContext.Current.Server.MapPath(VirtualDIR + "/Memos/");
string EvaluationUploadPath = HttpContext.Current.Server.MapPath(VirtualDIR + "/Evaluations/");
DateTime TimeStamp = DateTime.Now;
if (PostedFile != null)
{
if (!string.IsNullOrEmpty(Request["fileID"]))
{
int file_id = int.Parse(Request["fileID"]);
string FullFileName = uploadPath + "http://" + PostedFile.FileName;
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
PostedFile.SaveAs(FullFileName);
//下面這句代碼缺少的話,上傳成功后上傳隊列的顯示不會自動消失
//context.session[context.Session["userName"].ToString()] = filename;
//context.Response.Write(filename);
string content = SaveFileContent.GetFileContent(FullFileName);
}
else
{
Response.Write("0");
}
}
}
else
{
Response.Write("0");
}
}
順便把保存文件內容的方法寫在這個了(只適合.txt和Word文件啦)
public class SaveFileContent {
public SaveFileContent()
{ // // TODO: 在此處添加構造函數邏輯 //
}
public static string GetFileContent(object file_path)
{ string outText = string.Empty;
Word._application oWord = new Word.Application();
Word._Document oDoc;
object oMissing = System.Reflection.Missing.Value;
object format = WdSaveFormat.wdFormatDocument;
object VisiableWindows = false;
object Readonly = true;
try {
oWord.Visible = true;
object fileName = file_path;
oDoc = oWord.Documents.Open(ref fileName, ref format, ref Readonly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref VisiableWindows, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
outText = oDoc.Content.Text;
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref VisiableWindows);
//oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
return outText.ToString();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
return "";
}
}
就不展示結果啦~~
把3.2的具體參數放在這了,以便查找?。?/p>
http://blog.sina.com.cn/s/blog_5079086b0101fkmh.html
新聞熱點
疑難解答