我有這樣的事情:閱讀的文本 - C#和JavaScript
<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路徑不是文件的正確路徑。
我只是試圖從文件中獲取數據,然後用它做一些事情。
這是要做到這一點嗎?如果是這樣,我怎麼才能得到這個工作?
或者是否有更好的方法來做到這一點。
謝謝。
你得到了什麼樣的路徑..你是獲取Web服務器的路徑還是你期望的實際文件所在的絕對路徑?對於文件類型也不應該是像文本/「bla bla bla什麼其他選項,它期望在網絡上傳..? – MethodMan 2011-12-20 16:13:09
我凝視這個路徑:'c:\ fakepath \ myFile.txt'。沒有理解第二個問題 – Ovi 2011-12-20 16:19:31
你期待什麼路徑然後 – MethodMan 2011-12-20 16:22:38