cakephp下载链接后面的图像或媒体文件不起作用(cakephp download link behind the image or media files not working)
我正在开发一个cakephp 2.x ...我有一个表单,我从用户那里获取文件,然后我将文件保存到app / uploads / userid文件夹并将路径保存到db ...我是成功获取文件并将其显示给用户,但现在问题是在下载期间..我的意思是每当用户单击文件链接时,它会将用户重定向到某个未知链接
这是我的代码
public function uploadFile(){
if ($this->request->isPost()){ $this->loadModel('Audio'); $file = $this->request->data['Audio']['file']; $idUser = $this->Auth->user('idUser'); if ($file['error'] === UPLOAD_ERR_OK) { $id = String::uuid(); $name =$file['name']; $folder_url = APP.'uploads/'.$idUser; if(!is_dir($folder_url)) { mkdir($folder_url); } move_uploaded_file($file['tmp_name'], $folder_url.DS.$name); $this->request->data['Audio']['User_id'] = $idUser; $this->request->data['Audio']['filename'] = $file['name']; $this->request->data['Audio']['filesize'] = $file['size']; $this->request->data['Audio']['filemime'] = $file['type']; $this->Audio->save($this->request->data); return true; } } return false; } public function showfile(){ $this->loadModel('Audio'); $record = $this->Audio->find('first', array('conditions' => array('User_id' => $this->Auth->user('idUser')))); $this->set('file', $record); }
在我的视图页面上
<?php echo $this->Html->link($file['Audio']['filename'], APP . DS . 'uploads' . DS . $file['Audio']['User_id'] . DS . $file['Audio']['filename'] . DS . $file['Audio']['filemime']);?>
这是点击链接后的网址
/localhost/cakephp/media/C:/xampp/htdocs/cakephp/app//uploads/23/1011029_10152615392142588_1926259269_n.jpg/image/jpeg
我已经尝试过所有的东西让它工作对我来说都没有用..请我需要帮助..
i am working on a cakephp 2.x ... i have a form in which i am taking the file from the user and then i am saving fileinto the app/uploads/userid folder and saving the path into db ... i am successfully fetch the file and showing it to the user but now the problem is during downloading.. i mean whenever user click the file link it will redirected the user to some unknown link
here is my code
public function uploadFile(){
if ($this->request->isPost()){ $this->loadModel('Audio'); $file = $this->request->data['Audio']['file']; $idUser = $this->Auth->user('idUser'); if ($file['error'] === UPLOAD_ERR_OK) { $id = String::uuid(); $name =$file['name']; $folder_url = APP.'uploads/'.$idUser; if(!is_dir($folder_url)) { mkdir($folder_url); } move_uploaded_file($file['tmp_name'], $folder_url.DS.$name); $this->request->data['Audio']['User_id'] = $idUser; $this->request->data['Audio']['filename'] = $file['name']; $this->request->data['Audio']['filesize'] = $file['size']; $this->request->data['Audio']['filemime'] = $file['type']; $this->Audio->save($this->request->data); return true; } } return false; } public function showfile(){ $this->loadModel('Audio'); $record = $this->Audio->find('first', array('conditions' => array('User_id' => $this->Auth->user('idUser')))); $this->set('file', $record); }
on my view page
<?php echo $this->Html->link($file['Audio']['filename'], APP . DS . 'uploads' . DS . $file['Audio']['User_id'] . DS . $file['Audio']['filename'] . DS . $file['Audio']['filemime']);?>
here is the url after clicking the link
/localhost/cakephp/media/C:/xampp/htdocs/cakephp/app//uploads/23/1011029_10152615392142588_1926259269_n.jpg/image/jpeg
i have tried every thing to make it work both nothing works out for me .. please i need help ..
原文:https://stackoverflow.com/questions/17713634
最满意答案
您必须设置延迟加载,您可以通过设置dbContext来为仅特定的工作单元或所有工作单元执行此操作
dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false;
或者在DbContext的Ctor中设置它。
You must set off the lazy loading, you can do this for just a specific unit of work or for all by setting your dbContext Like
dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false;
or set it in Ctor of your DbContext.
相关问答
更多-
使用单个设备集合: public class Controller { [Key] public int Id { get; set; } public string Name { get; set; } public virtual List
Devices { get; set; } } ...并在需要处理或显示具有特定Status值的设备时对其进行过滤: controller.Devices.Where(d => d.Statu ... -
实体框架4 Code First自定义表映射Fluent API问题(Entity Framework 4 Code First Custom table mapping Fluent API issue)[2022-08-06]
您的地址属性不是虚拟的。 除非属性是虚拟的,否则延迟加载无法在POCO上工作。 Your address properties aren't virtual. Lazy loading can't work on a POCO unless the properties are virtual. -
你的问题在这里: this.Property(x => x.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 您应该将其设置为DatabaseGeneratedOption.Identity 。 在您的情况下,它将记录添加到具有相同ID的表。 Your problem is here: this.Property(x => x.Id) .HasDatabaseGeneratedOpt ...
-
首先 - 按惯例EF处理名称等于Id或EntityTypeName + Id属性是主键。 因此,您无需手动配置。 第二 - 如果您不希望表名为复数,只需从上下文中删除该约定,而不是为每个实体映射提供表名: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove
(); base.On ... -
什么是流畅的Nhibernate映射配置中引用的“实体”?(What is the “Entity” refering to in fluent Nhibernate Mapping Configuration?)[2021-08-11]
从程序集添加会添加实体所属类型的程序集中的所有映射。 AddFromAssembly将执行以下操作: typeof(TEntity).GetType().GetAssembly(); 这将获得Entity所属的程序集,然后它将使用反射来查找所有映射类。 而不是接受我的话为什么不下载源并自己看看:O) Add from assembly adds all mappings in the assembly that the type Entity belongs to. AddFromAssembly wil ... -
您必须设置延迟加载,您可以通过设置dbContext来为仅特定的工作单元或所有工作单元执行此操作 dbContext.Configuration.LazyLoadingEnabled = false; dbContext.Configuration.ProxyCreationEnabled = false; 或者在DbContext的Ctor中设置它。 You must set off the lazy loading, you can do this for just a specific ...
-
事实证明,在这种情况下你不需要Fluent API,你需要的是在具有InverseProperty属性的实体中DataAnnotate你的属性 [InverseProperty("AccountID")] Ladislav Mrnka在实体框架4.1 InverseProperty属性问题中有一个很好的答案 但是,如果有人知道如何正确地使用Fluent答案,我们非常感谢 Turns out you don't need Fluent API in this case, what you need is t ...
-
public class SomeEntity { public int SomeEntityId {get;set;} public string Name {get;set;} public ICollection
EntityProperties {get;set;} } public class EntityProperty { public int EntityPropertyId {get;set;} public str ... -
实体框架5代码优先中的一对一和一对多关系(Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First)[2024-02-09]
我相信以下内容对您有用 public class Address { public int AddressId { get; set; } public string AddressString { get; set; } } public class User { public int UserId { get; set; } public virtual ICollection Addresses { get; set; } } public cla ... -
您可以尝试如下所示。您错过了public关键词:D public class UserMap : EntityTypeConfiguration
{ public UserMap() { this.ToTable("User"); this.HasKey (p => p.Id); this.Property(u => u.UserPin).IsRequired().HasMaxLength(2000); } } Y ...