Jquery 使用ajax调用上载文件
我浏览了多个教程,但都没有帮助。 我有一个带有4个控件和选项卡面板引导主题的页面。 在页面的代码隐藏中,我要保存我上载的文件:Jquery 使用ajax调用上载文件,jquery,asp.net,ajax,twitter-bootstrap,webmethod,Jquery,Asp.net,Ajax,Twitter Bootstrap,Webmethod,我浏览了多个教程,但都没有帮助。 我有一个带有4个控件和选项卡面板引导主题的页面。 在页面的代码隐藏中,我要保存我上载的文件: [WebMethod] public static string UploadFile() { try { var file = HttpContext.Current.Request.Files["file"]; var filName = Path.GetFile
[WebMethod]
public static string UploadFile()
{
try
{
var file = HttpContext.Current.Request.Files["file"];
var filName = Path.GetFileName(file.FileName);
var filePlace = HttpContext.Current.Server.MapPath("Uppladdat") + "\\" + filName;
file.SaveAs(filePlace);
return filePlace;
}
catch (Exception)
{
return "";
}
}
对于ajax请求,我使用以下代码:
function uploadFileAjax(file) {
var formData = new FormData(file);
// formData.append("file", file);
$.ajax({
url: 'Wizard.aspx/UploadFile',
type: 'POST',
data: formData,
cache: false,
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
enctype: 'multipart/form-data',
success: function (response) {
console.log(response);
},
error: function (errorThrown) {
// Handle errors here
console.log('ERRORS: ' + errorThrown);
// STOP LOADING SPINNER
}
});
}
除了我非常需要的一个字符串外,它会返回页面html的成功结果。我还尝试使用“更新面板”执行一些操作,但它会刷新所有页面:
<asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="col-sm-3">
<input id="uppladdadFil" type="file" name="uppladdadFil" runat="server" />
</div>
<div class="col-sm-4">
<input id="Submit1" class="btn btn-default" type="submit" value="Bifoga" name="Submit1" runat="server" onserverclick="Submit1_ServerClick" />
<input type="hidden" name="HiddenField1" id="HiddenField1" runat="server" />
</div>
<div class="row top10">
<div class="col-sm-6">
<asp:Label ID="lblFilFelmeddelande" CssClass="label label-danger" runat="server" />
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Submit1" />
</Triggers>
</asp:UpdatePanel>
将非常感谢您对解决该任务的任何帮助。看起来该链接有帮助。至少我可以访问服务器端