在我的网站上我正在上传一个音频文件,该文件保存在项目文件夹中。然后在查看页面时,我想检查上传的文件是否存在于项目文件夹中。我该怎么检查呢。我用这个函数来获取URL并检查该文件中是否存在文件
public string GetAbsoluteUrl( string relativePath) { 尝试 { // 使用权限获取主机和端口 // 仅获取主机DnsSafeHost string host = HttpContext.Current.Request.Url.Authority; string protocol = HttpContext.Current.Request.Url.Scheme; return string .Format( {0}:// {1} / {2},protocol,host,relativePath.Trim()); } catch (例外情况) { throw ex; } }或者只使用HttpContext.Current.Server.MapPath +文件夹名称+文件名并检查文件是否存在..请帮帮我... 我也想知道什么;两者之间的区别
解决方案使用Server.MapPath:它将相对于您的网站路径(如〜/ uploads / MP3 /)转换为您可以在File.Exists方法: if (File.Exists(Server.MapPath( 〜/ Uploads / MP3 / + filename))) { ... 文件}
如果你打算放置,你只需要一个绝对的URL直接链接到客户端网页上的文件的永久链接(这不是一个好主意,因为这意味着该文件夹必须是公开可用的e)所示。
Hi, In my website i am uploading an audio file, which is saved in project folder. Then while viewing a page, i want to check whether the uploaded file exists in the project folder. How can i check it.Shall I used this function to get the URL and check whether file existed in that url
public string GetAbsoluteUrl(string relativePath) { try { //Get host and port by using Authority //Get host only by DnsSafeHost string host = HttpContext.Current.Request.Url.Authority; string protocol = HttpContext.Current.Request.Url.Scheme; return string.Format("{0}://{1}/{2}", protocol, host, relativePath.Trim()); } catch (Exception ex) { throw ex; } }Or just use HttpContext.Current.Server.MapPath + Folder name+ file name and check whether the file exists or not.. Please help me out... I also want to know what;s the difference between the two
解决方案 Use Server.MapPath: It converts a relative-to-your-website path like "~/uploads/MP3/" to a full path specification you can use in the File.Exists method: if (File.Exists(Server.MapPath("~/Uploads/MP3/" + filename))) { ... do something with the file }You would only need an absolute URL if you are planning on putting a permanent link to the file directly onto the clients webpage (which isn't a good idea, as it means the folder must be publically available).
更多推荐
HttpContext.Current.Server.MapPath和HttpContext.Current.Request.Url.Authority之间的区
发布评论