Asp.net ajax文件上传控件无法使用url重写
本文关键字:url 重写 控件 ajax net 文件 Asp | 更新日期: 2023-09-27 18:19:06
我有一个文件上传控件从asp.net Ajax控件工具包。我的web应用正在使用url重写。
我的ajax文件上传控制标记如下,
<cc1:AsyncFileUpload Width="200px" ID="fu" runat="server" OnClientUploadError="uploadError"
OnClientUploadStarted="StartUpload" OnClientUploadComplete="UploadComplete" CompleteBackColor="Lime"
UploaderStyle="Modern" ErrorBackColor="Red" ThrobberID="Throbber" OnUploadedComplete="AsyncFileUpload1_UploadedComplete"
UploadingBackColor="#66CCFF" />
服务器上的事件(后面的代码)如下,
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string extension = Path.GetExtension(fu.PostedFile.FileName).ToLower();
string fileType = null;
switch (extension)
{
case ".gif":
fileType = "image/gif";
break;
case ".jpg":
case ".jpeg":
case ".jpe":
fileType = "image/jpeg";
break;
case ".png":
fileType = "image/png";
break;
default:
lblStatus.Text = "<br />Error - invalid file type.<br />";
return;
}
System.Threading.Thread.Sleep(5000);
//string q = Decrypt(Request.QueryString["pname"].ToString());
string q = Request.QueryString["un"].ToString();
string sql = "update AppUser set Pic=@pic where ProfileName='" + q + "'";
SqlConnection con = new SqlConnection(constr);
byte[] imageBytes = new byte[fu.PostedFile.InputStream.Length + 1];
fu.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@pic", imageBytes);
con.Open();
int ret = cmd.ExecuteNonQuery();
if (ret > 0) { }
}
与ajax文件上传器相关的客户端事件如下,
function uploadError(sender, args) {
document.getElementById('lblStatus').innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
}
function StartUpload(sender, args) {alert("en");
document.getElementById('lblStatus').innerText = 'Uploading Started.';
}
function UploadComplete(sender, args) {
var filename = args.get_fileName();
var contentType = args.get_contentType();
var text = "Size of " + filename + " is " + args.get_length() + " bytes";
if (contentType.length > 0) {
text += " and content type is '" + contentType + "'.";
}
document.getElementById('lblStatus').innerText = text;
}
似乎是一个问题是, ajax扩展文件上传控制不工作与url重写。
我的代码涉及全局url重写。
protected void Application_BeginRequest(object sender, EventArgs e)
{
string CurrentUrl = HttpContext.Current.Request.Url.AbsoluteUri;
string[] s = CurrentUrl.Split('/');
string ActionName = CurrentUrl.Split('/')[s.Length - 1];
bool f = checkUserExist(ActionName);
if (f == true)
{
CurrentUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower();
s = CurrentUrl.Split('/');
ActionName = CurrentUrl.Split('/')[s.Length - 1];
}
string originalPath = HttpContext.Current.Request.Path.ToLower();
if (ActionName.Contains("AsyncFileUploadID"))
{
//string str = ActionName.Replace("alikhan/",@"/Auth/profile.aspx");//http://localhost:59287/auth/profile.aspx
//HttpContext.Current.RewritePath(originalPath.Replace(str, ActionName));
//HttpContext.Current.RewritePath(originalPath.Replace("Auth/profile.aspx", "Auth/profile.aspx"));
HttpContext.Current.RewritePath("/auth/profile.aspx");
}
else
{
//alikhan?AsyncFileUploadID=fu1&rnd=0728572010062635
if (!(ActionName.Contains("aspx") || ActionName.Contains("net") || ActionName.Contains(".") || ActionName.Contains("ashx")))
{
if (originalPath.Contains("/" + ActionName))
{
HttpContext.Current.RewritePath(originalPath.Replace("/" + ActionName, @"/Auth/profile.aspx?un=" + ActionName));
// HttpContext.Current.Response.Redirect(@"/Auth/profile.aspx?un=" + ActionName);
}
}
else
{
if (!(ActionName.Contains("ashx") || ActionName.Contains("aspx")))
{
// if (!ActionName.Contains("AsyncFileUploadID"))
{
try
{
HttpContext.Current.RewritePath(originalPath.Replace(ActionName, ActionName));
}
catch (Exception ex)
{// HttpContext.Current.RewritePath(originalPath.Replace("fp.aspx", "fp.aspx"));
}
}
}
//HttpContext.Current.Response.Redirect("fp.aspx",true);
}
}
}
我怎样才能使它工作。我如何使用ajax文件上传控制与url重写?
任何帮助都将不胜感激。
谢谢
我所需要做的就是修改
HttpContext.Current.RewritePath(string)
string必须被调整