【发布时间】:2019-08-06 02:52:02
【问题描述】:
此时我正在上传 2 个 txt 文件。在文件“A”中,我有一个字母列表,在文件“B”中,我有几个单词,我正在阅读这两个文件,并将它们添加到列表中。目标是根据“A”文件的字母来查找是否可以从“B”文件中组成单词。示例:
“A”文件的内容: 乙 乙 是的 ○ 磷 D R 乙 一个
“B”文件的内容: 波贝达, 钴
在这种情况下,在 POBEDA 中找到的单词
我正在使用网络服务来执行该过程
这是网络服务:
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string Leerdata()
{
var path = Directory.EnumerateFiles(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\", "*.txt");
List<string> list = path.ToList();
Parallel.ForEach(path, current =>
{
string[] lines = File.ReadAllLines(current);
list.AddRange(lines.ToList());
});
return String.Join(",", list);
}
}
这是控制器:
namespace NewShoreApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files)
{
if (ModelState.IsValid)
{
try
{
foreach (HttpPostedFileBase file in files)
{
if (file != null)
{
var ServerPath = Path.Combine(Server.MapPath("~/Data"), Path.GetFileName(file.FileName));
file.SaveAs(ServerPath);
}
}
ViewBag.FileStatus = "File uploaded successfully.";
}
catch (Exception)
{
ViewBag.FileStatus = "Error while file uploading.";
}
}
return View("Index");
}
}
}
这是模型:
namespace NewShoreApp.Models
{
public class Data
{
//
[DataType(DataType.Upload)]
[Display(Name = "Upload File")]
[Required(ErrorMessage = "Please choose file to upload.")]
public HttpPostedFileBase[] files { get; set; }
}
}
【问题讨论】:
-
你有什么问题?
-
@madreflection 检查你给我的字母我能不能做这个词
-
@Michael Randall 感谢您的推荐
-
关键是将其分解为两个单独的任务,从文本文件中读取所有文本,并比较两个字符串中的字符。
File.ReadAllText将为您提供第一个...(我修改了此评论是因为大量错别字)
标签: c# asp.net asp.net-mvc web-services asmx