文件上传
在上传页面添加:<iframe frameborder="0" scrolling="no" id="frameUpload" src="FileUpload.aspx" height="28px" width="400px"></iframe>
FileUpload.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="OpenChinese_SharedResources_FileUpload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="../JS/jquery.js"></script> <script type="text/javascript"> function Upload() { __doPostBack($('#hidbtn_submit').val(), ""); } function upFile() { return document.getElementById("<%=upFile.ClientID %>").value; } </script> </head> <body style="margin: 0 0 0 0"> <form id="form1" runat="server"> <div> <asp:FileUpload ID="upFile" runat="server" Width="380px" Height="28px"/> <asp:LinkButton ID="btn_submit" ClientIDMode="Static" runat="server" Text="" OnClick="btn_submit_Click" /> <input type="hidden" value="<%=btn_submit.UniqueID %>" id="hidbtn_submit" /> </div> </form> </body> </html>
上传文件:
string upFileName = localRoot + shareOriginalPath + "/" + saveName + ext;
// 判断存放原文件的路径是否存在
if (Directory.Exists(localRoot + shareOriginalPath) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(localRoot + shareOriginalPath);
}
UploadFile.PostedFile.SaveAs(upFileName);
protected void btn_submit_Click(object sender, EventArgs e) {
string filename = UpLoadFile(upFile, "upload");
if (!string.IsNullOrEmpty(filename))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "uploadfile", "window.top.prvimage('" + filename + "')", true);
}
}
function prvimage(upValue) { var upHFileUrl = document.getElementById("HFileUrl"); upHFileUrl.value = upValue; var Pic = document.getElementById('frameUploadPic').contentWindow.upFile(); if (Pic != "") { document.getElementById('frameUploadPic').contentWindow.Upload(); var end = upValue.substring(upValue.lastIndexOf(".") + 1); end = end.substring(0, end.indexOf("|")); if (end == "swf" || end == "flv" || end == "mp4" || end == "SWF" || end == "FLV" || end == "MP4") { document.getElementById('frameUploadPic').contentWindow.Upload(); } } else { //让按钮事件进行提交 __doPostBack($('#hidUid').val(), ""); } return false; }