我有一个用Angularjs和PHP(Slim Framework)编写的表单。 在表单中,用户需要能够上传文件(pdf),该文件将在提交表单后作为自动生成的电子邮件的附件(与phpmailer一起发送)发送。
我已经研究过我的问题的解决方案,但我找不到任何对我有用的东西。
有没有人知道如何解决这个问题?
答案 0 :(得分:0)
首先,您需要设置HTML。您还可以找到示例here。
<div popover="Open a previously saved project." popover-trigger="mouseenter" class="form-button" ng-file-select ng-model="file">Open Existing Project</div>
注意我这里有一个popover,你上传时出错,popover不会消失,我建议你不要使用它或者在这里发布一个解决方案;)
然后对为文件上传设置的变量使用监视。
//Watch file for changes to run open file dialog
$scope.$watch('file', function () {
//Only make the ajax call if there is a file to upload
if ($scope.file) {
ajaxFactory.openFileDialog($scope.file, setProjectData);
}
});
在我的情况下,我有一个调用MVC控制器的ajax包装器
/// <summary>
/// Save a file to the server
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult Upload(HttpPostedFileBase file)
{
var retObject = new JsonReturnObject();
try
{
// Read bytes from http input stream
BinaryReader b = new BinaryReader(file.InputStream);
var binData = b.ReadBytes((int)file.InputStream.Length);
retObject.StringPayload = NttCommon.GetString(binData);
retObject.Success = true;
b.Close();
b.Dispose();
}
catch (ArgumentNullException ane)
{
Log.Error("An ArgumentNullException occured while uploading to FileManagerController.", ane);
retObject.StringPayload = ane.InnerException.ToString();
}
catch (ArgumentOutOfRangeException aoore)
{
Log.Error("An ArgumentOutOfRangeException occured while uploading to FileManagerController.", aoore);
retObject.StringPayload = aoore.InnerException.ToString();
}
catch (DecoderFallbackException dfe)
{
Log.Error("An DecoderFallbackException occured while uploading to FileManagerController.", dfe);
retObject.StringPayload = dfe.InnerException.ToString();
}
catch (ArgumentException ae)
{
Log.Error("An ArgumentException occured while uploading to FileManagerController.", ae);
retObject.StringPayload = ae.InnerException.ToString();
}
catch (IOException ioe)
{
Log.Error("An IOException occured while uploading to FileManagerController.", ioe);
retObject.StringPayload = ioe.InnerException.ToString();
}
catch (ObjectDisposedException ode)
{
Log.Error("An ObjectDisposedException occured while uploading to FileManagerController.", ode);
retObject.StringPayload = ode.InnerException.ToString();
}
catch (Exception e)
{
Log.Error("An unknown exception occured while uploading to FileManagerController.", e);
retObject.StringPayload = e.InnerException.ToString();
}
return FormatJsonReturnResult.ReturnStandardJsonResult(retObject);
}
答案 1 :(得分:0)
我用uploadify解决了我的问题