Javascript 是否可以通过Jquery.ajax()方法使用path在webmethod中获取文件?
据我所知,使用Jquery/Ajax调用向webmethod发送文件是不可能的。但是如果我发送文件的路径呢?并使用该路径在webmethod中获取文件 客户端:Javascript 是否可以通过Jquery.ajax()方法使用path在webmethod中获取文件?,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,据我所知,使用Jquery/Ajax调用向webmethod发送文件是不可能的。但是如果我发送文件的路径呢?并使用该路径在webmethod中获取文件 客户端: $.ajax({ type: "POST", url: "Default.aspx/UploadFile", data: JSON.stringify({ name: $('#fileData').val(); }), contentTyp
$.ajax({
type: "POST",
url: "Default.aspx/UploadFile",
data: JSON.stringify({ name: $('#fileData').val(); }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
console.log("CallWM");
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
[WebMethod]
public static string UploadFile(string name)
{
byte[] buffer;
FileStream fileStream = new FileStream(@"D:\Untitled.png", FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return "a";
}
WebMethod:
$.ajax({
type: "POST",
url: "Default.aspx/UploadFile",
data: JSON.stringify({ name: $('#fileData').val(); }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
console.log("CallWM");
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
[WebMethod]
public static string UploadFile(string name)
{
byte[] buffer;
FileStream fileStream = new FileStream(@"D:\Untitled.png", FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return "a";
}
这行吗?我认为大多数现代浏览器不允许您获取文件的真实路径。有解决办法吗?没有,这不可行。
嗯,你说的是两件不同的事。您发送的文件路径将在服务器上解析。如果你想上传一个文件,你需要从客户端发送它。你搞混了很多
想象一下,您试图将此消息发送到服务器:C:\Users\yourName\Test.txt
,
您认为服务器将在哪里查找文件?服务器将在其本地驱动器上搜索
尝试使用默认方式上载文件。也许使用jQuery插件,你可以在后台发出请求。互联网将为您的场景提供大量教程。只要用谷歌搜索一下就可以了。不,这行不通。如果您想在不回发的情况下上载整个页面的文件,可以使用iframe并将提交表单的目标设置为此iframe虽然不确定,但您可以通过HttpPostedFileBase
foreach (string one in Request.Files)
{
HttpPostedFileBase file = Request.Files[one];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.ContentLength);
}
}
我相信您可以向webmethod发送一个文件来简化使用Base64的过程 例如,将文件读取为字节将字节转换为Base64
var bytes = File.ReadAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "filename.extension"));
然后可以转换为Base64
var stringBase64=Convert.ToBase64String(bytes);
然后在另一端,将接收到的base64字符串转换为字节
var bytes = Convert.FromBase64String(ReceivedString);
就这些是的,我有点怀疑服务器会在本地机器上查找文件。但是由于我的webmethod是在aspx的代码中,而不是在不同的Web服务上,所以我有一点希望。我在谷歌上搜索了很多。有很多插件,但我的场景是,我有一个表单,应该保存textboex,下拉列表和一个按钮点击文件。我不希望发生回发。找不到任何插件可以让我通过点击按钮将所有这些数据与文件一起发送。有趣的是,您将代码命名为“客户端”,但使用了“WebMethod”-而不是“WebMethod”,它应该读为“服务器端”,这将使WebMethod在服务器上运行的位置更加清晰