黄色, 我有两个字符串 - FileName和UString。我想通过下载按钮下载此文件。文件的路径必须是动态的,并且与FileName的故事相同 HomeController:
public FileResult GetPdf()
{
Read read = new Read(); //in Read class i have the data readed from XML (thats the file name and unique string.
string Ustring = read.VratUstring(); //UString - is the containing folder of .pdf
string FileName = read.VratFileName(); // Name of file
return File(@"E:/Miriam/"+ Ustring +"/" + FileName +".pdf", "application/pdf", FileName +".pdf");
}
按钮:
<div class="form-group">
<div class="content">
<input type="submit" value="" class="myButton" formaction="GetPdf" />
</div>
</div>
我从浏览器获得的结果是:
Soubor E:\ Miriam.pdf nebyl nalezen。 // thast意味着文件E:没有找到Miriam / .pdf。但我错过了ustring和文件名。 路径应如下所示: E:\仪\ dlvhkjclkjvhklcDKKF2 \ CelejmPDF.pdf
命名空间MVC_SAFE.Models { 课阅读 { public string JobName {get;组; } //工作名称 public int Index {get;组; } // verzion public string FileName {get;组; } // Cele jmeno * .pdf a * .xml public string Customer {get;组; } // Zakaznik 公共字符串URL {get;组; } // URL html stranky(odkaz z mailu) public string UString {get;组; } // unikatni klic pro html sooubor a adresar z eska 公共字符串Lang {get;组; } // Jazykova verze public DateTime Created {get;组; } // cas odeslani
//precte a vypise hodnoty vsech nodu (uzlu)
public void Precti(string ustring) //unikatni klic zadany zakaznikem
{
//nacteni vstupniho xml
List<Uzivatel> uzivatele = new List<Uzivatel>(); //pripojuju tridu uzivatel
using (XmlReader xr = XmlReader.Create(@"E:/Miriam/" + ustring + "/" + ustring + ".xml")) //cesta k *.xml souboru + hodnoty z xml jsou ulozene v xr
{
string jobName = ""; //Jmeno zakazky
int index = 0; //Verze korektury
string fileName = ""; //Cele jmeno *.pdf a *.xml
string customer = ""; //Zakaznik
string url = ""; //URL html stranky (odkaz z mailu)
string uString = ""; //unikatni klic pro html sooubor a adresar z eska
string lang = ""; //Jazykova verze
DateTime created = DateTime.Now; //cas odeslani
string element = ""; //element tagu
while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element) //
{
element = xr.Name; // název aktuálního elementu
}
else if (xr.NodeType == XmlNodeType.Text) //nacita hodnoty elementu
switch (element)
{
case "JobName":
jobName = xr.Value;
break;
case "Index":
index = int.Parse(xr.Value);
break;
case "FileName":
fileName = xr.Value;
break;
case "Customer":
customer = xr.Value;
break;
case "URL":
url = xr.Value;
break;
case "UString":
uString = xr.Value;
break;
case "Lang":
lang = xr.Value;
break;
case "Created":
created = DateTime.Parse(xr.Value);
break;
}
else if ((xr.NodeType == XmlNodeType.EndElement) && (xr.Name == "SAFE"))
uzivatele.Add(new Uzivatel(jobName, index, fileName, customer, url, uString, lang, created));
foreach (Uzivatel u in uzivatele)
{
Console.WriteLine(u);
}
JobName = jobName;
Index = index;
FileName = fileName;
Customer = customer;
URL = url;
ustring = uString;
Lang = lang;
Created = created;
}
}
}
public string VratJobName()
{ return JobName; }
public int VratIndex()
{ return Index; }
public string VratFileName()
{ return FileName; }
public string VratCustomer()
{ return Customer; }
public string VratUstring()
{return UString;}
}
}
这里是动作结果,其中我将数据存储到viewBag。我不明白为什么它的工作和getprd没有
[HttpPost]
public ActionResult Potvrzeni(ParsovaniXml2Web parsovaniXml) //zobrayi ustring zadany klientem
{
if (ModelState.IsValid)//potvrzeni vyhledavaciho klice + sem chci vlozit parsovani z xml do db a nacteni dat z db
{
Read read = new Read();
read.Precti(parsovaniXml.UString);
ViewBag.Index = read.Index;
ViewBag.UString = parsovaniXml.UString;
ViewBag.Zakaznik = read.Customer;
ViewBag.Cas = read.Created;
ViewBag.JobName = read.JobName;
ViewBag.Filename = read.FileName;
}
return View();
}
为什么我无法从我的两个字符串中获取数据? 亲切的问候 克拉马斯
答案 0 :(得分:0)
在Precti
方法的末尾设置属性时,变量名称不正确。
Precti
方法采用ustring
参数,用于查找XML文件。
在switch / case语句中,您将局部变量uString
设置为找到的值:
case "UString":
uString = xr.Value;
break;
在方法结束时,输入变量将设置为找到的值:
ustring = uString;
要将属性设置为您从XML文件中读取的值,该行应为:
UString = uString;
只是一些建议:
ustring
有三种外壳变体。您应该考虑为至少一个变量使用更具描述性的名称,而不是依赖于单个字符大小写差异。更具描述性的名称不仅可以使您的代码更具可读性,还可以大大减少这些错误。