您好我正在尝试在fancybox中实现fileupload。更具体地说,我希望用户像在stackoverflow上一样上传他们的图像。
问题:当我在fancybox中上传图片时,图片的扩展名会丢失。但是,在没有fancybox效果的情况下做同样的事情会导致成功提交和检索。
aspx代码:
<div class="poc1">
<asp:Image ID="Image1" runat="server" />
<div class="changePic">
<a class="fancybox" href="#shadow"> Change Pic </a>
</div>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<div id="shadow" style="display:none;width:300px;">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" />
<br /><br />
</div>
jquery:
<script type="text/javascript">
$(".fancybox").fancybox({
parent: "form:first",
openEffect : 'none',
closeEffect : 'none',
afterLoad : function() {
this.content = this.content.html();
}
});
</script>
aspx.cs cdode:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
using System.Data.SqlClient; using System.Configuration; using System.Web.Security;
using System.IO;
public partial class Default3 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string virtualFolder = "~/UserPics/";
string physicalFolder = Server.MapPath(virtualFolder);
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
//save old pic vlaue in string
SqlCommand cmdSelect = new SqlCommand("SELECT [UserPicUrl] FROM [UserDetails] WHERE ([UserId] = @UserId)", myConnection);
cmdSelect.Parameters.AddWithValue("@UserId", currentUserId);
myConnection.Open();
string oldPicUrl = (string)(cmdSelect.ExecuteScalar());
string oldPic = Server.MapPath(oldPicUrl);
Label1.Text = "old Pic :- " + oldPic;
//save new pic
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
String ImageUrl = virtualFolder + fileName + extension;
Label2.Text = "New Pic :- " + ImageUrl;
Image1.ImageUrl = ImageUrl;
//update new pic in db
SqlCommand cmdUpdate = new SqlCommand("UPDATE [UserDetails] SET [UserPicUrl] = @UserPicUrl WHERE [UserId] = @UserId", myConnection);
cmdUpdate.Parameters.AddWithValue("@UserId", currentUserId);
cmdUpdate.Parameters.AddWithValue("@UserPicUrl", ImageUrl);
//delete old pic
cmdUpdate.ExecuteNonQuery();
myConnection.Close();
myConnection.Dispose();
File.Delete(oldPic);
} }
请在我错的地方说清楚,已经做了太多但却无法找到解决方案。或者是否还有其他简单方法可以在文件上传条件周围设置灯箱效果。