我在mvc4中编写了一个操作方法,它从链接下载文件,然后从该文件将数据上传到数据库。我正在使用Web客户端类下载该文件。现在,在这个阶段,我想知道是否有任何合适的方法来检查url中的错误,并停止下载文件,在映射的位置保持前一个文件的完整性。以下是我如何将文件下载到我的文件夹中:
public class testController : Controller
{
public class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var req = base.GetWebRequest(address);
req.Timeout = 15000000;
return req;
}
}
public ActionResult index()
{
System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/App_Data/TempBrdcepPSC/PscData"));
foreach (FileInfo csvfile in di.GetFiles())
{
csvfile.Delete();
}
MyWebClient webClient = new MyWebClient();
webClient.DownloadFile("https://www.google.com.pk/", Server.MapPath("~/App_Data/New_folder/Summary.csv"));
return View();
}
}
现在,我想知道是否有合适的方法检查url 中的错误
您可以使用以下方法检查您的Uri是否有效
public static bool URLValidatorMethod(string strURL)
{
Uri uri;
return Uri.TryCreate(strURL, UriKind.RelativeOrAbsolute, out uri);
}
Q(TryCreate
实际做什么?
A( 使用指定的System.String实例和System.UriKind.创建新的System.Uri,并返回System.Boolean值,如果成功创建了System.Uri则为true,否则为false。
您可以使用检查url是否有http
或https
return Uri.TryCreate(strURL, UriKind.RelativeOrAbsolute, out uri) && uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps;
或
public static bool URLValidatorMethod(string strURL)
{
return Uri.IsWellFormedUriString(strURL, UriKind.RelativeOrAbsolute); ;
}
Q(IsWellFormedUriString
实际做什么?
A( 通过尝试用字符串构造URI来指示字符串的格式是否正确,并确保该字符串不需要进一步转义。
在映射位置
首先通过上述方法检查您的url是否有效,如果有效,则将其保存到文件夹中,否则忽略
通过此操作,您以前下载的文件将不再受到影响,并且它在您的地图上是安全的,如
string url = "https://www.google.com.pk/";
if (URLValidatorMethod(url))
{
webClient.DownloadFile(url, Server.MapPath("~/App_Data/New_folder/Summary.csv"));
}
else
{
//Do code here that you want
}
编辑:
try
{
string url = "https://www.google.com.pk/";
if (URLValidatorMethod(url))
{
webClient.DownloadFile(url, Server.MapPath("~/App_Data/New_folder/Summary.csv"));
}
else
{
//Do code here that you want
}
}
catch (Exception)
{
//Do code here if you want to execute if exception occurred
}
finally
{
//Do the code here that you want either exception occurred or not
//Means this code run always if exception comes or not
}
或
如果发生特定类型的异常或错误,您可以修改捕获块
catch (Exception ex)
{
//You may check any particular exception and write your code
if (ex.GetType() == typeof(ArgumentNullException))
{
//Do code here
}
else
if (ex.GetType() == typeof(WebException))
{
//Do code here
}
else
if (ex.GetType() == typeof(NotSupportedException))
{
//Do code here
}
}
编辑:
参考上面的详细答案,这是对我有效的解决方案,如果它能帮助某人的话:
创建一个临时文件夹在那里下载你的文件,如果下载过程中没有出现错误,它会将文件从临时文件夹复制到原来的位置并完成其余的工作。否则,如果出现任何错误,将发送一封指示错误的电子邮件,而其余的代码将在不导致实际代码流错误的情况下执行。
[HttpGet]
public ActionResult index(int OpCode)
{
try
{
System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder"));
foreach (FileInfo csvfile in di.GetFiles())
{
csvfile.Delete();
}
MyWebClient webClient1 = new MyWebClient();
webClient1.DownloadFile("http://example.php", Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/Summary.csv"));
}
catch (Exception)
{
return RedirectToAction("ImportSendMail");
}
var absolutePath = HttpContext.Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/" + "Summary.csv");
if (System.IO.File.Exists(absolutePath))
{
System.IO.DirectoryInfo di2 = new DirectoryInfo(Server.MapPath("~/App_Data/TempPSC/PscData"));
foreach (FileInfo csvfile in di2.GetFiles())
{
csvfile.Delete();
}
String sourceFile = Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/Summary.csv");
String destFile = Server.MapPath("~/App_Data/TempPSC/PscData/abc" + ".csv");
System.IO.File.Copy(sourceFile, destFile, true);
}
{
//Rest of the default code that you want to run
}
return View();
}
检查错误:DownloadFile
方法抛出三个异常Ref-
ArgumentNullException
WebException
(这应该适合您的用例(NotSupportedException
将代码放入try-catch块。
保持前一个文件的完整性:我建议,首先将文件下载到临时文件夹中,如果没有异常,则从您刚刚下载的临时文件夹中的最近文件中替换前一个。
快乐编码!!!
您可以通过这种方式检查HTTP错误,该函数还可以检查URL是否存在。
private bool HttpValidation(string URL)
{
Uri urlToCheck = new Uri(URL);
WebRequest request = WebRequest.Create(urlToCheck);
request.Timeout = 15000;
WebResponse response;
try
{
response = request.GetResponse();
}
catch (Exception)
{
return false; //url does not exist or have some error
}
return true;
}
URL必须以http://
或https://
的形式提供。
例如。
if(HttpValidation("https://www.google.com.pk"))
{
webClient.DownloadFile("https://www.google.com.pk/",Server.MapPath("~/App_Data/New_folder/Summary.csv"));
}
如果你想检查url是否有效,那么你可以尝试
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
我希望这对你有帮助。
创建一个临时文件夹在那里下载你的文件,如果下载过程中没有出现错误,它会将文件从临时文件夹复制到原始位置并完成其余的工作。否则,如果出现任何错误,将发送一封指示错误的电子邮件,而其余的代码将执行,而不会在实际的代码流中造成错误。
[HttpGet]
public ActionResult index(int OpCode)
{
try
{
System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder"));
foreach (FileInfo csvfile in di.GetFiles())
{
csvfile.Delete();
}
MyWebClient webClient1 = new MyWebClient();
webClient1.DownloadFile("http://example.php", Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/Summary.csv"));
}
catch (Exception)
{
return RedirectToAction("ImportSendMail");
}
var absolutePath = HttpContext.Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/" + "Summary.csv");
if (System.IO.File.Exists(absolutePath))
{
System.IO.DirectoryInfo di2 = new DirectoryInfo(Server.MapPath("~/App_Data/TempPSC/PscData"));
foreach (FileInfo csvfile in di2.GetFiles())
{
csvfile.Delete();
}
String sourceFile = Server.MapPath("~/App_Data/TempPSC/PscData/Temp_folder/Summary.csv");
String destFile = Server.MapPath("~/App_Data/TempPSC/PscData/abc" + ".csv");
System.IO.File.Copy(sourceFile, destFile, true);
}
{
//Rest of the default code that you want to run
}
return View();
}