<p><textarea cols="50" rows="15" name="code" class="javascript:nogutter"> function uploadFile(selectFileId, fileCName) {
$.ajax({
type: "POST",
url: "AjaxService.aspx",
data: { cPath: $("#" + selectFileId).val(), fileCName: fileCName },
success: function (data) {
var strValue = data.split("|");
$("#" + selectFileId).attr("fileSName", strValue[1]);
alert("上传成功|" + $("#" + selectFileId).attr("fileSName"));
},
error: function (data) {
alert("上传失败|" + data.split("|")[1]);
}
});
}
$("#templatetabs-1-btn").bind("click", function () {
uploadFile("templatetabs-1_upimg", "content"); //内容
});</textarea></p>
<p>后台代码:</p>
<p><textarea cols="50" rows="15" name="code" class="c-sharp:nogutter"> private void UpLoadFile()
{
try
{
string cPath = Request.Form["cPath"];
string sPath = "../data/cms/temp/";
//服务器端用来做前置符号
string fileCName = Request.Form["fileCName"];
FileInfo file = new FileInfo(cPath);
string tempSName = Guid.NewGuid().ToString() + file.Extension;
string fileSName = string.Format("{0}_{1}", fileCName, tempSName);
///创建WebClient实例
WebClient myWebClient = new WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = CredentialCache.DefaultCredentials;
FileStream fs = new FileStream(cPath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(toFile, "PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(Server.MapPath(sPath) + fileSName, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
postStream.Dispose();
Response.Write(string.Format("上传成功|{0}", fileSName));
}
catch (Exception ex)
{
Response.Write(string.Format("上传失败|{0}", ex.Message));
}
}</textarea></p>
$.ajax({
type: "POST",
url: "AjaxService.aspx",
data: { cPath: $("#" + selectFileId).val(), fileCName: fileCName },
success: function (data) {
var strValue = data.split("|");
$("#" + selectFileId).attr("fileSName", strValue[1]);
alert("上传成功|" + $("#" + selectFileId).attr("fileSName"));
},
error: function (data) {
alert("上传失败|" + data.split("|")[1]);
}
});
}
$("#templatetabs-1-btn").bind("click", function () {
uploadFile("templatetabs-1_upimg", "content"); //内容
});</textarea></p>
<p>后台代码:</p>
<p><textarea cols="50" rows="15" name="code" class="c-sharp:nogutter"> private void UpLoadFile()
{
try
{
string cPath = Request.Form["cPath"];
string sPath = "../data/cms/temp/";
//服务器端用来做前置符号
string fileCName = Request.Form["fileCName"];
FileInfo file = new FileInfo(cPath);
string tempSName = Guid.NewGuid().ToString() + file.Extension;
string fileSName = string.Format("{0}_{1}", fileCName, tempSName);
///创建WebClient实例
WebClient myWebClient = new WebClient();
//设定windows网络安全认证 方法1
myWebClient.Credentials = CredentialCache.DefaultCredentials;
FileStream fs = new FileStream(cPath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(toFile, "PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(Server.MapPath(sPath) + fileSName, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
postStream.Dispose();
Response.Write(string.Format("上传成功|{0}", fileSName));
}
catch (Exception ex)
{
Response.Write(string.Format("上传失败|{0}", ex.Message));
}
}</textarea></p>
本文介绍了一个使用JavaScript实现的文件上传功能及其后端处理流程。前端通过AJAX进行异步文件上传,后端采用C#接收并处理上传的文件,包括重命名、保存等操作。
1839

被折叠的 条评论
为什么被折叠?



