我是Web API的新手,我创建了一个Web API,它包含下载文件的方法。当我从网络浏览器请求我的API下载任何文件时,它会下载文件,但我不知道单击按钮后它将如何工作。我不明白我在哪里错了,下面是我的代码。
在API中下载方法
[HttpGet]
public HttpResponseMessage DownloadContent(int? contentId, int? learnerId)
{
var classMethodName = this.GetType().Name + "|" + System.Reflection.MethodBase.GetCurrentMethod().Name;
//var uniqueId = Guid.NewGuid().ToString();
newLog.append(uniqueId, classMethodName, 75);
//HttpResponseMessage response;/
// string rootPath = "";
try
{
// for download command get path of the file
if (contentId != null && contentId.Value > 0)
{
if (learnerId == null && learnerId.Value <= 0) throw new Exception(Constants.learnerId_Is_Required);
newLog.append(uniqueId, classMethodName + " | Content Id :" + contentId + ", Learner Id :" + learnerId, 75);
// maintain the download record in LearnerContentM and increment content download count in TeacherContentM
new DownloadService().SaveContentDownload(contentId, learnerId, newLog, uniqueId);
DirectoryInfo dir = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Files/ContentDocuments/"));
newLog.append(uniqueId, classMethodName + " | Directory :" + dir.Name, 75);
var fileName = Path.GetFileName(DownloadService.GetContentFileNameByID(contentId));
string fullPath = dir + "" + fileName;
FileInfo file = new FileInfo(fullPath);
string ContentType = "";
string extainsion = Path.GetExtension(fullPath);
switch (extainsion)
{
case ".pdf":
ContentType = "application/pdf";
break;
case ".PDF":
ContentType = "application/pdf";
break;
default:
ContentType = "text/html";
break;
}
newLog.append(uniqueId, classMethodName + " | Content Type :" + ContentType, 75);
if (String.IsNullOrEmpty(fullPath))
return Request.CreateResponse(HttpStatusCode.BadRequest);
string reqBook = fullPath;
string bookName = fileName;
//converting Pdf file into bytes array
var dataBytes = File.ReadAllBytes(reqBook);
//adding bytes to memory stream
var dataStream = new MemoryStream(dataBytes);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(dataStream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
return result;
}
else
{
newLog.append(uniqueId, classMethodName + " | Learner and Content Id's are empty.", 75);
throw new Exception(Constants.contentId_Is_Required);
}
}
catch (Exception ex)
{
newLog.append(uniqueId, classMethodName + " | Exception :" + ex.Message + "\t Inner Exception : " + ex.InnerException, 75);
HttpResponseMessage httpResponse = new HttpResponseMessage();
httpResponse.StatusCode = HttpStatusCode.BadRequest;
httpResponse.Content = null;
httpResponse.ReasonPhrase = ex.Message;
return httpResponse;
}
}
如果我从浏览器请求它可以下载文件,则上面的代码可以正常工作,但是当我尝试从其后面的代码中调用它时出错。下面是我单击按钮时的代码。
if (e.CommandName == "download")
{
// get content id from row hidden field
int contentId = getIdFromHiddenField(e.Item.FindControl("hidContentId") as HiddenField);
// get learner id from hidden field
int learnerId = getIdFromHiddenField(hidLearnerId);
// for download command get path of the file
HiddenField hidFileName = e.Item.FindControl("hidFileName") as HiddenField;
if (hidFileName != null)
{
string serviceData = string.Format("api/Bilyak/DownloadContent?contentId={0}&learnerId={1}", contentId, learnerId);
HttpResponseMessage oData = DatabaseServices.DownloadContent(serviceData);
if (oData.IsSuccessStatusCode)
{
// Don't understand what should i do here....
}
}
}
我不知道我是对是错。任何帮助将不胜感激...