我有这样的事情:
<form action="" id="fileUpload">
<input type="file" id="fileTest"/>
<input type="button" id="saveFile"/> </form>
供用户上传文件。
然后我有这个js:
$('#saveFile').click(function () {
PageMethods.ReadFile($('#fileTest').val());
});
在我的C#代码中,我这样做:
[WebMethod]
public static void ReadFile(string path)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(path))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
但是发送到webMethod的路径不是文件的正确路径。
我只是想从文件中获取数据,然后对其进行处理。
这是这样做的方法吗?如果是这样,我如何使它工作?
还是有更好的方法来做到这一点。
谢谢。
参考方案
您正在尝试访问服务器上的客户端文件($('#fileTest')
)。这是不可能的。
取而代之的是,您需要将上传表单过帐到Web服务器并在那端进行处理。
javascript popupwindow之后的行如何工作? - javascript好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…
C#和JavaScript-转义单引号字符 - c#我有一个XML文件,存储的字符串如下所示:Error: The form's name field was left empty! 现在,XML文件中的此消息已在各个位置使用。例如,有时绑定到aspx控件,有时显示在JavaScript警报中。为了转义单引号字符,我对消息进行了如下修改:Error: The form\'s name fi…
Javascript-Urls的奇怪字符串比较行为 - javascript最近,在编写我无法理解的javascript时遇到了字符串比较的问题。我从完全相同的网址创建了两个字符串,当我比较它们时返回false,但是在重新分配相同的字符串后,比较返回true。这是我的示例:var str1 = "http://google.com/"; var str2 = "http://google.com/…
Javascript-从当前网址中删除查询字符串 - javascript单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …